title: c# - Progress Bar [Threading] Cross-thread operation not valid: Control 'progressBar' accessed from a thread other than the thread it was created on
body: <p>I am stuck with the above issue. I got lot of solutions but none of them working for me.
  Please find herewith my code</p>
  
  <pre><code>private void btnRunQuery_Click(object sender, EventArgs e)
      {
          try
          {
  
              Thread ProcessThread = new Thread(Process);
              ProcessThread.Start();
  
              Thread.CurrentThread.Join();
          }
          catch
          {
              Debug.WriteLine("Error in model creation");
              Console.WriteLine("Error in model creation");
          }
          finally
          {
              //dsModel = null;
          }
      }
  
  
  private void Process()
      {
  
          using (var dataContext = new IControlerDataContext())
          {
              dataContext.EnlistTransaction();
  
              IItemPropertyRepository itemPropertyRepository = ObjectContainer.Resolve&lt;IItemPropertyRepository&gt;();
              IList&lt;ItemProperty&gt; itemPropertyCollection = itemPropertyRepository.LoadAll();
              totalCount = itemPropertyCollection.Count;
              currentCount = 0;
  
              foreach (var itemProperty in itemPropertyCollection)
              {
                  try
                  {
                      message = string.Empty;
                      currentCount++;
                      if (itemProperty.DeletedDate == null &amp;&amp; (itemProperty.MetaItemProperty.ValueType == MetaItemPropertyValueType.MetaItemTableProperty || itemProperty.MetaItemProperty.ValueType == MetaItemPropertyValueType.MetaItemTableMultiSelectProperty))
                      {
                          //Property refresh issue in only applicable for table and multitable property.
                          //Need to filter the itemproperty for Table and multitable select property.
                          message = ProcessItemProperty(itemProperty);
                          //txtLogDetails.Text += message + Environment.NewLine;
                          //txtLogDetails.Refresh();
                          //txtLogDetails.ScrollToCaret();
                      }
                      //Log(message);
                      //progressBar.Value = (Int32)(currentCount * 100 / totalCount);
                      //progressBar.Refresh();
                      Invoke(new MyDelegate(ShowProgressBar), (Int32)(currentCount * 100 / totalCount));
  
                  }
                  catch (Exception ex)
                  {
                      txtLogDetails.Text += "EXCEPTION ERROR : " + itemProperty.Id.ToString();
                      dataContext.RollBackTransaction();
                  }
              }
              dataContext.CompleteTransaction();
          }
      }
  
   delegate void MyDelegate(int percentage);
      private void ShowProgressBar(int percentage)
      {
          progressBar.Value = percentage;
          progressBar.Refresh();
          //txtLogDetails.Text = message;
      }
  </code></pre>
  
  <p>When it is executing " Invoke(new MyDelegate(ShowProgressBar), (Int32)(currentCount * 100 / totalCount));" this line it goes out of scope. It goes inside and never came back. and also havn't caught in exception.</p>
  
  <p>Can anyone please help me out from this?</p>
  
  <p>Thanks,
  Mahesh</p>