CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Posts
    51

    [RESOLVED] Cross Thread Function Calls

    I have a function being executed by a timer. When I tried to make that function up date a label got an error about cross thread calls. Looking into this i found a solution from microsoft:
    http://msdn.microsoft.com/en-us/library/ms171728.aspx
    Code:
     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)";
        }
    However i cant access SetTextCallback. Dose anyone know where this object is located or if there is a better one for setting text values for labels?

    I think SetTextCallback is just a delegate, and if so could i make my own to call the function in the other thread?

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Cross Thread Function Calls

    here is a threading example....

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace ThreadingUIUpdate
    {
        public partial class Form1 : Form
        {
            // delegate is used to write to a UI control from a non-UI thread
            public delegate void UpdateTextCallback(string message);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(TestThread));
                thread.Start();
            }
    
            private void UpdateText(string message)
            {
                textBox1.Text = message;
            }
    
            private void TestThread()
            {
                for (int i = 0; i <= 10000; i++)
                {
                    Thread.Sleep(1000);
                    textBox1.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { i.ToString() });
                }
            }
        }
    }

  3. #3
    Join Date
    Oct 2008
    Posts
    51

    Re: Cross Thread Function Calls

    Absolutely Awesome! Not only dose that code solve my problem but it shows how to use delegates for this purpose.

    Thanks,

  4. #4
    Join Date
    Jun 2003
    Posts
    258

    Re: Cross Thread Function Calls

    Hi,

    The first way is still the way to do it. If for some reason you call TestThread() from the form's thread, eclipsed4utoo's method will throw an Invocation exception. These are really hard to track down. In a controlled situation this is easy to not do, but in a large app things aren't always this clear. An even better method is to create an extension method (if you're on vs2008) for all textboxes -

    Code:
            public static void ThreadSafeSetText(this TextBox t, string text)
            {
                if (t.InvokeRequired)
                {
                    t.Invoke(new MethodInvoker(delegate() { ThreadSafeSetText(t, text); }));
                }
                else
                {
                    t.Text = text;
                }
            }
    This will keep you from filling up your forms with functions to deal with this situation. Also, this is the proper way to use an anonymous delegate. Hope this helps. I have a separate extension methods class for all of the things I use very often (ToDouble(), ToInt(), UpdateProgressBarFromThread(), etc).

    Please hit the the Thanks button if this was useful!
    Last edited by diehardii; December 4th, 2008 at 10:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured