Posted by: Zeeshan Amjad | August 7, 2009

Threading in WPF Revisited


Previously we discussed how can we perform threading in WPF application with the help of Dispatcher object and calling its BeginInvoke method. We discuss two different techniques one is using delegate and anonymous method.

Now we are going to pass parameter in the thread. Here is a simple method to create parameterized thread.

  1:         Thread th = new Thread(new ParameterizedThreadStart(ThreadFun));
  2:         th.Start("This is a message from Parameter Thread");
  3: 

Now our thread function is little bit change. Now our thread function accept one parameter object. It means theoretically we can pass any thing in the thread.

  1: public void ThreadFun(object str)

Here is the complete code of the program.

  1: using System;
  2: using System.Windows;
  3: using System.Threading;
  4: using System.Windows.Threading;
  5: 
  6: public class MyWindow : Window
  7: {
  8:     private delegate void MyDelegate(string str);
  9: 
 10:     public MyWindow()
 11:     {        
 12:         Title = "Parameterized Thread Example";
 13:     }
 14: 
 15:     public void SetContent(string str)
 16:     {
 17:         Thread.Sleep(3000);
 18:         Content = str;
 19:     }
 20: 
 21:     public void ThreadFun(object str)
 22:     {
 23:         MyDelegate setContent = SetContent;
 24: 
 25:         Dispatcher.BeginInvoke(DispatcherPriority.Normal,
 26:             setContent, (string)str);
 27:     }
 28: 
 29:     public void StartThread()
 30:     {
 31:         Thread th = new Thread(new ParameterizedThreadStart(ThreadFun));
 32:         th.Start("This is a message from Parameter Thread");
 33:     }
 34: }
 35: 
 36: public class wpf 
 37: {
 38: 
 39:     [STAThread]
 40:     public static void Main()
 41:     {
 42:         MyWindow win = new MyWindow();
 43: 
 44:         win.StartThread();
 45: 
 46:         Application app = new Application();
 47:         app.Run(win);
 48:     }
 49: }

This program will display “This is a message from Parameter Thread” after three seconds on the main screen. We can do the same thing with anonymous functions. Here is the code to do the same thing with anonymous method.

  1: using System;
  2: using System.Windows;
  3: using System.Threading;
  4: using System.Windows.Threading;
  5: 
  6: public class MyWindow : Window
  7: {
  8:     public MyWindow()
  9:     {        
 10:         Title = "Parameterized Thread Example";
 11:     }
 12: 
 13:     public void SetContent(string str)
 14:     {
 15:         Thread.Sleep(3000);
 16:         Content = str;
 17:     }
 18: 
 19:     public void ThreadFun(object str)
 20:     {
 21: 
 22:         Dispatcher.BeginInvoke(DispatcherPriority.Normal,
 23:             (ThreadStart)delegate
 24:             {
 25:                 Thread.Sleep(3000);
 26:                 Content = (string)str;
 27:             }
 28:         );
 29:     }
 30: 
 31:     public void StartThread()
 32:     {
 33:         Thread th = new Thread(new ParameterizedThreadStart(ThreadFun));
 34:         th.Start("This is a message from Parameter Thread");
 35:     }
 36: }
 37: 
 38: public class wpf 
 39: {
 40: 
 41:     [STAThread]
 42:     public static void Main()
 43:     {
 44:         MyWindow win = new MyWindow();
 45: 
 46:         win.StartThread();
 47: 
 48:         Application app = new Application();
 49:         app.Run(win);
 50:     }
 51: }
Advertisements

Responses

  1. […] threading with Lambda expression We saw two examples of threading here and here. But in both cases we are creating delegate to perform the threading. For small tasks it looks like […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: