How to communicate between two UI Threads

Posted on 2008-11-12
Last Modified: 2012-05-05
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));


            for (int i = 0; i < 100; i++)


                mform.label1.Text = i.ToString();   // <-----   Cross Thread Violation Here





Open in new window

Question by:JRumana
    LVL 12

    Expert Comment

    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.
    LVL 12

    Expert Comment

    (was) -> (way)
    LVL 53

    Accepted Solution

    Perhaps you should take a look at this article: Updating the UI from a Secondary Thread

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now