[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1413
  • Last Modified:

How to communicate between two UI Threads

In a c# windows application i have 2 forms namely form1 and form2 in the form one if i click a button it will show the form 2 with a label. I want to pass a value from form1 to form2 label control. But i get cross thread violation error. I searched the net but i all the discussions are about communication between UI Thread and Backgroundworker Thread. How i can communicate between this Two UI Threads?
private void btnStart_Click(object sender, EventArgs e)
        {
            form2 mform = new form2();
 
 
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(mform.Show));
 
            t.Start();
            for (int i = 0; i < 100; i++)
            {
                mform.label1.Text = i.ToString();   // <-----   Cross Thread Violation Here
                System.Threading.Thread.Sleep(10000);
               
            }
        }

Open in new window

0
JRumana
Asked:
JRumana
  • 2
1 Solution
 
andrewjbCommented:
Same was as if you're calling from a background worker...

You need to call .Invoke on the "other" form and pass a method to set the label value etc.
0
 
andrewjbCommented:
(was) -> (way)
0
 
DhaestCommented:
Perhaps you should take a look at this article: Updating the UI from a Secondary Thread
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
http://weblogs.asp.net/justin_rogers/articles/126345.aspx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now