title: Lock hangs when called from UI to System.Threading.Thread
body: <p><strong>EDIT:</strong>
  please see question history, for unchanged question in order not to invalidate comments.</p>
  
  <p>I am clicking button that executes certain codes and  it creates a thread (System.Threading.Thread). When I reclick button which starts process it hangs and freezes ui. What could be the reason?</p>
  
  <pre><code>public partial class ucLoader : UserControl
  {
      //lock object for whole instance of class ucLoader
      private object lockUcLoader = new object();
  
      //bringing info from ui
      private void btnBringInfo_Click(object sender, EventArgs e)
      {
          lock (lockUcLoader)
          {
              btnBringInfo_PerformClick(false);
          }
      }
  
      //using this method because it could be called when even button not visible
      internal void btnBringInfo_PerformClick(bool calledFromBandInit)
      {
          lock (lockUcLoader) //HANGS HERE when called multiple times and ui freeze as well
          //by the way I am using (repetitive) lock, because this method also called independently from btnBringInfo_Click
          {
              //...
              this.btnLoad_PerformClick();
          }
      }
  
      //Another button perform click that could be triggered elsewhere when even button not visible
      private void btnLoad_PerformClick()
      {
          lock (lockUcLoader) //I am using (repetitive) lock, because this method also called independently from btnBringInfo_PerformClick
          {
              //...
              Run();  
          }
      }
  
      //method for creating thread which System.Threading.Thread  
      private void Run()
      {
         lock (lockUcLoader)  //Maybe this lock is NOT REQUIRED, as it is called by only btnLoad_PerformClick(), could you please confirm?
          {
              //some code that thread can be killed when available,  you can ingore this two lines as they are irrelevant to subject, I think
              Source = new CancellationTokenSource();
              Token = Source.Token;
              var shell = new WindowsShell();
              Thread = new Thread((object o) =&gt;
              {
                  //...
                  var tokenInThread = (CancellationToken)o;
                  exitCode =TaskExtractBatchFiles(cls, shell, exitCode);
  
  
                   using (var logEnt = new logEntities())
                  {
  
                          //Do some db operation
                          //...
                          this.Invoke((MethodInvoker)delegate
                          {
                              //do some ui update operation
                              //...
                          });
                      }
              }
              Thread.Start(Token);
          }      
      } 
  
      public void Progress(string message)
      {
          Invoke((MethodInvoker)delegate  //ATTENTION HERE see below picture Wait occurs here
          {
              if (message != null &amp;&amp; message.Trim() != string.Empty)
              {
                  this.txtStatus.AppendText(message + Environment.NewLine);
              }
          });
      }       
  }       
  </code></pre>
  
  <p>In order to avoid get closed question, what my question is how can I prevent
  below method can be accesses with out lock from background thread and ui thread</p>
  
  <pre><code>public void Progress(string message)
          {
              Invoke((MethodInvoker)delegate  //ATTENTION HERE see below picture Wait occurs here
              {
                  if (message != null &amp;&amp; message.Trim() != string.Empty)
                  {
                      this.txtStatus.AppendText(message + Environment.NewLine);
                  }
              });
          }   
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/rY5ub.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rY5ub.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ZVuMx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZVuMx.png" alt="enter image description here"></a></p>