Mega Code Archive

 
Categories / C# Tutorial / Thread
 

Use the QueueUserWorkItem method to queue a task and supply the data for the task

using System; using System.Threading; public class MyValue {     public string MyString;     public int Value;     public MyValue(string text, int number) {         MyString = text;         Value = number;     } } public class Example {     public static void Main() {         MyValue ti = new MyValue("{0}", 42);         if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {                 Thread.Sleep(1000);             Console.WriteLine("Main thread exits.");         }         else {             Console.WriteLine("Unable to queue ThreadPool request.");          }     }     static void ThreadProc(Object stateInfo) {         MyValue ti = (MyValue) stateInfo;         Console.WriteLine(ti.MyString, ti.Value);      } }