SIGN UP MEMBER LOGIN:    
ARTICLE

Cross Thread Communication in Windows Forms: Part II

Posted by Suchit Khanna Articles | Windows Forms C# June 30, 2011
In this article we will continue learning about cross thread communication in Windows Forms.
Reader Level:
Download Files:
 


In this article we will continue learning about cross thread communication in Windows Forms. In the last Article we saw how we can use the BackgroundWorker class to perform cross thread communication. Today we will see how to do the same using Invoke/BeginInvoke methods of Windows Forms controls.

We will make use of a simple "Thread" class to create threads to perform our processor-intensive work and then marshal the result back to the UI thread.

To begin let us declare a variable:

private Thread m_workThread    = null;

Let us define a method that will simulate a long running task and will be called on a separte thread:

private void InvokeReqMethod(object args)
{
    Thread.Sleep(5000);
    if (richTextBox1.InvokeRequired)
    {
        richTextBox1.BeginInvoke(new InvokeDelegate(InvokeMarshalled), args);
    }
}

As you see in the above method we simulate a long running task by calling Sleep() on the thread that invokes that method, and then we use the InvokeRequired property of the UI control, which determines whether it is safe to access the control from a different thread (as it is not, since UI controls are thread unsafe) this will return true always and want us to marshal the call back to UI thread in order to safely access the UI control.
For that we specify another method "InvokeMarshalled" that will be called by InvokeDelegate and it will also marhsal the call back to the UI thread.

private void InvokeMarshalled(object args)
{
    EnableButtons(true);
    progressBar1.Visible = false;
    richTextBox1.AppendText("Marshalled using " + Convert.ToString(args));
}

Next let us define an event handler for the InvokeMarshal button click handler:

private void btnInvoke_Click(object sender, EventArgs e)
{
    EnableButtons(false);
    progressBar1.Visible = true;
    m_workThread = new Thread(new ParameterizedThreadStart(InvokeReqMethod));
    m_workThread.Start("Invoke");
}

Let us verify our implementation, again I set a break point in InvokeReqMethod method and another in InvokeMarshalled method respectively:

CTCWinFor1.gif

Now hit F5 and press InvokeMarshal button on UI, once the debugger is hit:

CTCWinFor2.gif

Once the debug point is hit, you can check in the Threads window again that the current position indicates that the process is in a thread different from the main UI thread.

Next hit F5 and let execution proceed to the method that is called with InvokeDelegate:

CTCWinFor3.gif

The current cursor position now indicates that after BeginInvoke() was called, the method is marshaled back and is being processed on the UI thread. So we have now seen another way to perform cross thread communication in Windows Forms.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor