Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C# 4.0

Updating Your Form from Another Thread without Creating Delegates for Every Type of Update

Rate me:
Please Sign up or sign in to vote.
4.85/5 (85 votes)
8 Oct 2010CPOL1 min read 283.2K   5.4K   242   59
Updating your form from another thread without creating delegates for every type of update

Download SimpleThreadSafeCall.zip - 43.07 KB

Introduction

Threads are nice when you need to do stuff in the background without causing your application to hang. Often you want to show the progress of the load that is being handled. In many occasions, that's a progressbar but in some you want to show in detail what is being done. In the first situation where it is only needed to update a progress bar, a backgroundworker can be used. The background worker sends thread safe events. In that event, you can update your progress bar. In the other situations where you want to show more information of what is being done, you need to call the form from within the other thread. If you do that, you will get an invalid crossthreadcall exception. Many articles that discuss the problem explain how to solve the problem by creating delegates and invoking them.

Example: How to: Make Thread-Safe Calls to Windows Forms Controls

C#
delegate void SetTextCallback(string text);
private void ThreadProcSafe()
{
    // Wait two seconds to simulate some background work
    // being done.
    Thread.Sleep(2000);

    string text = "Written by the background thread.";
    // Check if this method is running on a different thread
    // than the thread that created the control.
    if (this.textBox1.InvokeRequired)
    {
        // It's on a different thread, so use Invoke.
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke
            (d, new object[] { text + " (Invoke)" });
    }
    else
    {
        // It's on the same thread, no need for Invoke
        this.textBox1.Text = text + " (No Invoke)";
    }
}
// This method is passed in to the SetTextCallBack delegate
// to set the Text property of textBox1.
private void SetText(string text)
{
    this.textBox1.Text = text;
} 

I probably don't have to mention this is a lot of code for one simple textbox update.

An easy alternative

Finally, I found a quick and elegant solution to update a form from another thread. Thanks to some great feedback in the comments i was able to futher perfect the implementation.  The code is as follows:

C#
lblProcent.SafeInvoke(d => d.Text = "Written by the background thread");
progressBar1.SafeInvoke(d => d.Value = i);

//or call a methode thread safe. That method is executed on the same thread as the form
this.SafeInvoke(d => d.UpdateFormItems("test1", "test2"));

A threadSafe getter is also available. The getter knows what the return type is, so casting is not necessary.

C#
string textboxtext=textbox.SafeInvoke(d=>d.textbox.text); 

The function SafeInvoke are extension methods.

C#
        public static TResult SafeInvoke<t,>(this T isi, Func<t,> call) where T : ISynchronizeInvoke
        {
            if (isi.InvokeRequired) { 
                IAsyncResult result = isi.BeginInvoke(call, new object[] { isi }); 
                object endResult = isi.EndInvoke(result); return (TResult)endResult; 
            }
            else
                return call(isi);
        }

        public static void SafeInvoke<t>(this T isi, Action<t> call) where T : ISynchronizeInvoke
        {
            if (isi.InvokeRequired) isi.BeginInvoke(call, new object[] { isi });
            else
                call(isi);
        }
</t></t></t,></t,>
I hope this helps

History

  • 17/01/2010 Article published
  • 30/01/2010 Sample project added and article updated with the comments of philippe dykmans
  • 8/10/2010 Sample project changed and article updated with the comments of philippe dykmans  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPlease make a demo application. Pin
muharrem28-Jan-10 0:05
muharrem28-Jan-10 0:05 
GeneralRe: Please make a demo application. Pin
Michael Demeersseman30-Jan-10 0:40
Michael Demeersseman30-Jan-10 0:40 
QuestionLittle remark on naming Pin
Loic Berthollet25-Jan-10 23:59
Loic Berthollet25-Jan-10 23:59 
AnswerRe: Little remark on naming Pin
Michael Demeersseman30-Jan-10 0:39
Michael Demeersseman30-Jan-10 0:39 
GeneralError : Invalid expression term '=&gt;' Pin
Deagle 0.520-Jan-10 23:15
Deagle 0.520-Jan-10 23:15 
GeneralRe: Error : Invalid expression term '=&gt;' Pin
Michael Demeersseman30-Jan-10 0:31
Michael Demeersseman30-Jan-10 0:31 
GeneralRe: Error : Invalid expression term '=&gt;' Pin
Deagle 0.530-Jan-10 2:58
Deagle 0.530-Jan-10 2:58 
GeneralGood idea, with a small adaptation Pin
philippe dykmans19-Jan-10 6:04
philippe dykmans19-Jan-10 6:04 
You don't use the generic type identifier T in the function bodies. Means you're only using T to constrain parameter 'form' to be of type Form. Therefore you might as well substitute T by Form alltogether. Then, since cross-thread issues go for both Forms and Controls, and since Form is derived from Control, you could make the functions more useful by tying them to the Control type. Thus covering Form and Control at the same time. This would be the result:

public static void SafeThreadAction( this Control ctl, Action call )
{
ctl.BeginInvoke( call );
}


public static Y SafeThreadGet<Y>( this Control ctl, Func<Y> call )
{
IAsyncResult result = ctl.BeginInvoke( call );
object result2 = ctl.EndInvoke( result );
return (Y) result2;
} 


At the same time, your idea gave me the most compact solution for the problem until now. So thanks for that!

Regards,
Philippe

Philippe Dykmans
Software developpement
Advanced Bionics Corp.

GeneralRe: Good idea, with a small adaptation Pin
Michael Demeersseman21-Jan-10 0:33
Michael Demeersseman21-Jan-10 0:33 
GeneralRe: Good idea, with a small adaptation [modified] Pin
Michael Demeersseman30-Jan-10 0:06
Michael Demeersseman30-Jan-10 0:06 
GeneralRe: Good idea, with a small adaptation Pin
philippe dykmans15-Feb-10 9:00
philippe dykmans15-Feb-10 9:00 
QuestionGreat idea, how about this (slightly) modified version? Pin
Thomas Hjorslev19-Jan-10 3:35
Thomas Hjorslev19-Jan-10 3:35 
AnswerRe: Great idea, how about this (slightly) modified version? Pin
Michael Demeersseman19-Jan-10 4:07
Michael Demeersseman19-Jan-10 4:07 
GeneralRe: Great idea, how about this (slightly) modified version? Pin
Thomas Hjorslev19-Jan-10 7:11
Thomas Hjorslev19-Jan-10 7:11 
GeneralVB.net Pin
steackbrit18-Jan-10 13:53
steackbrit18-Jan-10 13:53 
GeneralRe: VB.net Pin
Michael Demeersseman19-Jan-10 0:47
Michael Demeersseman19-Jan-10 0:47 
GeneralRe: VB.net Pin
pinx4-Feb-10 0:53
pinx4-Feb-10 0:53 
GeneralRe: VB.net Pin
Michael Demeersseman4-Feb-10 0:57
Michael Demeersseman4-Feb-10 0:57 
GeneralGreat! Pin
xliqz17-Jan-10 2:54
xliqz17-Jan-10 2:54 
GeneralRe: Great! Pin
Michael Demeersseman19-Jan-10 0:51
Michael Demeersseman19-Jan-10 0:51 
GeneralNice one Pin
Dan Mos17-Jan-10 2:40
Dan Mos17-Jan-10 2:40 
GeneralRe: Nice one Pin
Michael Demeersseman19-Jan-10 0:51
Michael Demeersseman19-Jan-10 0:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.