Thursday, June 19, 2014

ThreadPool - QueueUserWorkItem

ThreadPool.QueueUserWorkItem

You can't use the Task Parallel Library if you're targeting an earlier version of the .NET Framework (prior to 4.0). Instead, you must use one of the older constructs for entering the thread pool: ThreadPool.QueueUserWorkItem and asynchronous delegates. The difference between the two is that asynchronous delegates let you return data from the thread. Asynchronous delegates also marshal any exception back to the caller.

QueueUserWorkItem

To use QueueUserWorkItem, simply call this method with a delegate that you want to run on a pooled thread:
static void Main()
{
  ThreadPool.QueueUserWorkItem (Go);
  ThreadPool.QueueUserWorkItem (Go, 123);
  Console.ReadLine();
}
 
static void Go (object data)   // data will be null with the first call.
{
  Console.WriteLine ("Hello from the thread pool! " + data);
}

Our target method, Go, must accept a single object argument (to satisfy the WaitCallback delegate). This provides a convenient way of passing data to the method, just like with ParameterizedThreadStart. Unlike with Task,QueueUserWorkItem doesn't return an object to help you subsequently manage execution. 

We must use WaitCallback. At MSDN, WaitCallback is described as a delegate callback method to be called when the ThreadPool executes. It is a delegate that "calls back" its argument.
WaitCallback
You can use WaitCallback by simply specifying the "new WaitCallback" syntax as the first argument to ThreadPool.QueueUserWorkItem. You don't need any other code to make this approach effective.


public partial class MainWindow : Form
{
    // This is the delegate that runs on the UI thread to update the bar.
    public delegate void BarDelegate();
 
    // The form's constructor (autogenerated by Visual Studio)
    public MainWindow()
    {
                InitializeComponent();
    }
 
    // When a buttom is pressed, launch a new thread
    private void button_Click(object sender, EventArgs e)
    {
                // Set progress bar length.
                progressBar1.Maximum = 6;
                progressBar1.Minimum = 0;
 
                // Pass these values to the thread.
                ThreadInfo threadInfo = new ThreadInfo();
                threadInfo.FileName = "file.txt";
                threadInfo.SelectedIndex = 3;
 
                ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), threadInfo);
    }
 
    // What runs on a background thread.
    private void ProcessFile(object a)
    {
                // (Omitted)
                // Do something important using 'a'.
 
                // Tell the UI we are done.
                try
                {
                    // Invoke the delegate on the form.
                    this.Invoke(new BarDelegate(UpdateBar));
                }
                catch
                {
                    // Some problem occurred but we can recover.
                }
    }
 
    // Update the graphical bar.
    private void UpdateBar()
    {
                progressBar1.Value++;
                if (progressBar1.Value == progressBar1.Maximum)
                {
                    // We are finished and the progress bar is full.
                }
    }
}







No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code