Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Threads are a very good thing, I think. But, working with them is very hard and needs a lot of knowledge and training. The main problem is when we want to access shared resources from two other threads which can cause undesirable effects.</p> <p>Consider classic example: you have a two threads which get some items from a shared list and after doing something they remove the item from the list.</p> <p>The thread method that is called periodically could look like this:</p> <pre><code>void Thread() { if (list.Count > 0) { /// Do stuff list.RemoveAt(0); } }</code></pre> <p>Remember that the threads, in theory, can switch at any line of your code that is not synchronized. So if the list contains only one item, one thread could pass the <code>list.Count</code> condition, just before <code>list.Remove</code> the threads switch and another thread passes the <code>list.Count</code> (list still contains one item). Now the first thread continues to <code>list.Remove</code> and after that second thread continues to <code>list.Remove</code>, but the last item already has been removed by the first thread, so the second one crashes. That's why it would have to be synchronized using <code>lock</code> statement, so that there can't be a situation where two threads are inside the <code>if</code> statement.</p> <p>So that is the reason why UI which is not synchronized must always run in a single thread and no other thread should interfere with UI.</p> <p>In previous versions of .NET if you wanted to update UI in another thread, you would have to synchronize using <code>Invoke</code> methods, but as it was hard enough to implement, new versions of .NET come with <code>BackgroundWorker</code> class which simplifies a thing by wrapping all the stuff and letting you do the asynchronous stuff in a <code>DoWork</code> event and updating UI in <code>ProgressChanged</code> event.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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