Chapter 4 - Multithreading With Windows Forms
CoderSource.net
Chapter 4 - Multithreading With Windows Forms - Article by muskad202
Level: BeginnerType: Article
Rating: Page: 1 of 1

Date: 4/7/2006 12:00:00 AM

Environment: Windows, .Net, Visual Studio .Net 2005

Chapter 4 – Multithreading with Windows Forms

You might come across a situation where you need to write a multithreading Windows Application, where multiple threads make updates to the UI.

The main purpose of this chapter is to inform you regarding one important issue you need to take care of.

Create a new Windows Application Project. To the main form, add a textbox, named textBox1. Also add a button named button1. In the event handler for button1 (the Click event handler), add the following code:

            Thread t = new Thread(new ThreadStart(foo));

            t.Start();

Define the function foo as:

        void foo()

        {

            textBox1.Text = "Hullo";

        }

Run the application. Click on the button. If you’re running from within the IDE, in Debug Mode – you’ll see an exception getting thrown – “Cross thread operation not valid”. What does this mean? In .NET 2.0, only the thread that creates a UI control is allowed to update it. In our application, one thread created the textbox control and placed it on the form, but another thread is trying to update it (by modifying its Text property).

So how do we get around this? You need to modify the foo () function as shown below:

        void foo()

        {

            textBox1.Invoke(new new_foo_delegate(new_foo));

        }

You also need the code below:

        public delegate void new_foo_delegate();

        void new_foo()

        {

            textBox1.Text = "Hullo";

        }

I won’t go into what delegates are, since that is outside the scope of this tutorial. I’ll just give a brief explanation of what the above code does. Basically, what we’re doing is, we’re informing the runtime, that we have a function called new_foo(), which we want to run on the same thread as the thread which created the textbox1 control. This is done with the help of the Invoke() function. The Invoke() function takes as parameter a delegate, which is a pointer to the function that will actually update the control; and executes that function in the same thread that created the control. Problem solved!

You need to follow this process whenever you have multiple threads updating the UI. Create a function that does the actual update, create a delegate for it, and then from the place where you want the UI to be updated, call the Invoke() function on the corresponding control.


Attachments

Project Files Multithreading with Windows Forms

1

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us