German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to Update a Windows Form Control from a Background Thread

Written by kamal on Jun 01, 2007
How to invoke a Control from with in the Thread using Visual C#

Explanation:

Windows Forms controls can only execute on the thread on which they were created, that is, they are not thread-safe. If you want to get or set properties, or call methods, on a control from a background thread, the call must be marshaled to the thread that created the control.

Create and start the Thread

When a thread is created, a new instance of the Thread class is created using a constructor that takes the ThreadStart delegate as its only parameter. However, the thread does not begin executing until the Start method is invoked. When Start is called, execution begins at the first line of the method referenced by the ThreadStart delegate.

private Thread timerThread;

timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();

Calling a Function from the Thread

MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's invoke method, or when you need a simple delegate but don't want to define one yourself. When you create a MethodInvoker delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

BeginInvoke executes the given delegate on the thread that owns this Control's underlying window handle. The delegate is called asynchronously and this method returns immediately. You can call this from any thread, even the thread that owns the control's handle. If the control's handle does not exist yet, this will follow up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, BeginInvoke will throw an exception. Exceptions within the delegate method are considered untrapped and will be sent to the application's untrapped exception handler.

// This Background Thread is called from the ThreadStart Delegate.
public void ThreadProc()
{
    try
    {
        MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
        while (true)
        {
            this.BeginInvoke(mi);
            Thread.Sleep(500) ;
        }
    }
}

// This function is called from the Background Thread
private void UpdateProgress()
{
    if (progressBar1.Value == progressBar1.Maximum)
    {
        progressBar1.Value = progressBar1.Minimum ;
    }
    progressBar1.PerformStep() ;
}

Visitors/Readers Comments
(for questions please use The Forum)



chuck
Great tip, been looking for sample code like this for three or four days.  Nice method, compact, and beats hunting for a "DoCmd" equivalent in c#.  Thanks for the great tip and code.  chucki

27/08/2007 22:34:19 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)