Wednesday, April 22, 2009

Thread related topic

A. ParameterizedThreadStart delegate

Like the non-parmater ThreadStart delegate:

Thread newThread=new Thread(new ThreadStart(Work));
newThread.Start();
static void Work() {...;}

The ParameterizedThreadStart delegate is used like the following:

Thread newThread=new Thread(new ParameterizedThreadStart(WorkWithParameter));
newThread.Start("Hello");
static void WorkWithParameter(object o)
{
string info=(string)o;
....
}

B.Set Thread Priority

Thread thread1=new Thread(new ThreadStart(Work1));
Thread thread2=new Thread(new ThreadStart(Work2));
//we didn't set thread1 priority, it is set
//as default:System.Threading.ThreadPriority.Normal
thread2.Priority = System.Threading.ThreadPriority.Highest;
thread1.Start();
thread2.Start();
There are 5 Priotity Enumeration: Highest,AboveNormal, Normal, BelowNormal, Lowest

No comments: