How to improve performance using .NET multi-threading


I have a mash-up application that get pages from different sites. After noticing a performance issue with it, I added a thread for each url connection to improve performance.
– I choose background thread method ThreadPool.QueueUserWorkItem because it’s more efficient and less overhead than normal Thread.Start().
– You need to create custom thread parameter class because
– QueueUserWorkItem accepts only 1 additional parameter beside WaitCallback.
– we need to pass 2 additional parameters: a normal function parameter and a wait handle (to let parent thread know when it’s done).

Here are the steps:
– create custom thread parameter class: MyThreadPara
– create thread function: ProcessUrl
– main code:
– create AutoResetEvent array for wait handles
– in the loop:
– create 1 wait handle for each thread and save it into AutoResetEvent array
– instantiate MyThreadPara with url & wait handle
– call ThreadPool.QueueUserWorkItem with ProcessUrl and thpara. Notes: ProcessUrl will be casted to Waitcallback; you don’t have to do new WaitCallback(ProcessUrl).
– synchonize all thread via WaitHandle.WaitAll

The below codes will demonstrate how to do it:
Before:
foreach (UrlEntry de in urls)
{
UrlEntity url = de.Value;
GetUrl(url); // non thread call
}

After adding thread:

AutoResetEvent[] waitHandles = new AutoResetEvent[urls.Count];
int i = 0;
foreach (UrlEntry de in urls)
{
UrlEntity url = de.Value;
waitHandles[i] = new AutoResetEvent(false);
MyThreadPara thpara = new MyThreadPara(url, waitHandles[i]);
ThreadPool.QueueUserWorkItem(ProcessUrl, thpara); // more efficient than Thread.Start()
i++;
}
WaitHandle.WaitAll(waitHandles);

ProcessUrl function:

void ProcessUrl(object data)
{
MyThreadPara p = (MyThreadPara)data;
UrlEntity url = p.url;
GetUrl(url);
p.e.Set(); // this thread is done -> notify parent thread
}

MyThreadPara class:
class MyThreadPara
{
public UrlEntity url;
public AutoResetEvent autoResetEvent;
public MyThreadPara(UrlEntity u, AutoResetEvent a)
{
url = u;
autoResetEvent = a;
}
}

Advertisements
How to improve performance using .NET multi-threading

One thought on “How to improve performance using .NET multi-threading

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