Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your first attempt is pretty good, but the thread continued to exist even after the application exits, because you didn't set the <code>IsBackground</code> property to <code>true</code>... here is a simplified (and improved) version of your code:</p> <pre><code>MyObject myObject = this.MyObject; Thread t = new Thread(()=&gt; { Thread.Sleep(1000); // wait a second (for a specific reason) DoTheCodeThatNeedsToRunAsynchronously(); myObject.ChangeSomeProperty(); }); t.IsBackground = true; t.Start(); </code></pre> <p>With regards to the thread safety: it's difficult to tell if your program functions correctly when multiple threads execute simultaneously, because you're not showing us any points of contention in your example. It's <strong>very</strong> possible that you will experience concurrency issues if your program has contention on <code>MyObject</code>.</p> <p>Java has the <code>final</code> keyword and C# has a corresponding keyword called <code>readonly</code>, but neither <code>final</code> nor <code>readonly</code> ensure that the state of the object you're modifying will be consistent between threads. The only thing these keywords do is ensure that you do not change the reference the object is pointing to. If two threads have read/write contention on the same object, then you should perform some type of synchronization or atomic operations on that object in order to ensure thread safety.</p> <h2>Update</h2> <p>OK, if you modify the reference to which <code>myObject</code> is pointing to, then your contention is now on <code>myObject</code>. I'm sure that my answer will not match your actual situation 100%, but given the example code you've provided I can tell you what will happen:</p> <p><strong>You will <em>not</em> be guaranteed which object gets modified:</strong> it can be <code>that.MyObject</code> or <code>this.MyObject</code>. That's true regardless if you're working with Java or C#. The scheduler may schedule your thread/timer to be executed before, after or during the second assignment. If you're counting on a specific order of execution, then you have to do <em>something</em> to ensure that order of execution. Usually that <em>something</em> is a communication between the threads in the form of a signal: a <code>ManualResetEvent</code>, <code>Join</code> or something else. </p> <p>Here is a join example:</p> <pre><code>MyObject myObject = this.MyObject; Thread task = new Thread(()=&gt; { Thread.Sleep(1000); // wait a second (for a specific reason) DoTheCodeThatNeedsToRunAsynchronously(); myObject.ChangeSomeProperty(); }); task.IsBackground = true; task.Start(); task.Join(); // blocks the main thread until the task thread is finished myObject = that.MyObject; // the assignment will happen after the task is complete </code></pre> <p>Here is a <code>ManualResetEvent</code> example:</p> <pre><code>ManualResetEvent done = new ManualResetEvent(false); MyObject myObject = this.MyObject; Thread task = new Thread(()=&gt; { Thread.Sleep(1000); // wait a second (for a specific reason) DoTheCodeThatNeedsToRunAsynchronously(); myObject.ChangeSomeProperty(); done.Set(); }); task.IsBackground = true; task.Start(); done.WaitOne(); // blocks the main thread until the task thread signals it's done myObject = that.MyObject; // the assignment will happen after the task is done </code></pre> <p>Of course, in this case it's pointless to even spawn multiple threads, since you're not going to allow them to run concurrently. One way to avoid this is by not changing the reference to <code>myObject</code> after you've started the thread, then you won't need to <code>Join</code> or <code>WaitOne</code> on the <code>ManualResetEvent</code>. </p> <p>So this leads me to a question: why are you assigning a new object to <code>myObject</code>? Is this a part of a for-loop which is starting multiple threads to perform multiple asynchronous tasks?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload