Note that there are some explanatory texts on larger screens.

plurals
  1. PODeadlocking lock() method
    primarykey
    data
    text
    <p>I'm facing a deadlock, my code structure is similar to this:</p> <pre><code>private delegate void UpdateControlDelegate(string value); public void UpdateControl(string value) { if (txtAddress.InvokeRequired) { txtAddress.Invoke(new UpdateControlDelegate(UpdateControl), value); } else { txtAddress.Text = value; // This is in GroupBox1 txtValue.Text = value; // This is in GroupBox2 } } class ThreadHandler { List&lt;string&gt; _list = new List&lt;string&gt;(); object _criticalSection = new object(); public ThreadHandler() { new Thread(new ThreadStart(Run)).Start(); } public static ThreadHandler _threadHandler = null; public static ThreadHandler GetThreadHandler() { if (_threadHandler == null) { _threadHandler = new ThreadHandler(); } return _threadHandler; } public void Run() { while (true) { // some code lock (_criticalSection) { foreach (string str in _list) { // some Code } } // some code Thread.Sleep(SomeTime); } } public void AddItem(string item) { lock (_criticalSection) { _list.Add(item); } } public void RemoveItem(string item) { lock (_criticalSection) { _list.Remove(item); } } } </code></pre> <p>But using the same code, I just modified the UpdateControl method like this:</p> <pre><code>private delegate void UpdateControlDelegate(string value); public void UpdateControl(string value) { if (InvokeRequired) { BeginInvoke(new UpdateControlDelegate(UpdateControl), value); } else { txtAddress.Text = value; // This is in GroupBox1 txtValue.Text = value; // This is in GroupBox2 } } </code></pre> <p>This is working fine. What is the problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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