C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C# Source Code



Cross thread calling - simple and easy!

A very simple and self-explanatory example of how to use delegates to call methods (invoke) from other threads - as well as passing objects! E.g, updating the Textbox on a form from a separate thread without error.

Submitted By: Imdsm
Actions:
Rating:
Views: 6,218

Language: C#

Last Modified: February 2, 2010
Instructions: No extra instructions

Snippet


  1. /**
  2. * Adam K Dean <www.adamsmediadesign.co.uk>
  3. * Cross Thread Calling
  4. * E.g, updating the Textbox on a form from a separate thread
  5. **/
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. using System.Threading;
  16.  
  17. namespace TestProject
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         // params delegate - used for parametrized thread starting
  22.         private delegate void TestDel(object obj);
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.  
  30.         // params button
  31.         private void button2_Click(object sender, EventArgs e)
  32.         {
  33.             // create a delegate pointing to function testparams
  34.             TestDel td = new TestDel(testparams);
  35.  
  36.             // create the new thread, using a params threadstart
  37.             Thread th = new Thread(new ParameterizedThreadStart(td));
  38.             th.Start("params");
  39.         }
  40.  
  41.  
  42.         // this uses params, polymorphed into an object
  43.         private void testparams(object obj)
  44.         {           
  45.             // check if we need to invoke
  46.             // to avoid errors such as writing to a form in a different
  47.             // thread
  48.             if (InvokeRequired)
  49.             {
  50.                 // again, recreate the delegate, invoke is like
  51.                 // Call(func, params)
  52.                 TestDel method = new TestDel(testparams);
  53.                 Invoke(method, obj);
  54.                 return;
  55.             }
  56.  
  57.             // and cast it back to a string again
  58.             string text = (string)obj;
  59.             textBox1.Text = text;
  60.         }
  61.  
  62.         // This is seperate code:
  63.         // if you want to call threads without params
  64.  
  65.         // no params button - invoke without any params
  66.         private void button1_Click(object sender, EventArgs e)
  67.         {
  68.             // create a standard thread
  69.             Thread th = new Thread(new ThreadStart(testmethod));
  70.             th.Start();
  71.         }
  72.  
  73.         // no parameters
  74.         private void testmethod()
  75.         {
  76.             // again, check if we're on the right thread, if not, invoke
  77.             if (InvokeRequired)
  78.             {
  79.                    // slightly different now, as we dont need params
  80.                 // we can just use MethodInvoker
  81.                 MethodInvoker method = new MethodInvoker(testmethod);
  82.                 Invoke(method);
  83.                 return;
  84.             }
  85.  
  86.             // Of course, no params so do whatever you need
  87.             textBox1.Text = "WORKS!";
  88.         }
  89.     }
  90. }
  91.  
  92.  

Copy & Paste


Comments

manauwer 2010-03-17 00:54:55

very nice


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.