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;
}
}

How to improve performance using .NET multi-threading

Passing MCTS Exam 70-536

I’m so happy that I’ve just passed the MCTS 70-536 (Microsoft .NET Framework 2.0 – Application Development Foundation) test on the first try with 930 score (700 is  passing score) .

The study was hard for me as there were so many distractions. I had to force myself to bring study material everywhere to remind me to study. I studied while getting oil change, get tire changed, waiting for haircut… It  takes awhile for me to get into study mode. But once I got “in the zone” mode, the study gets a lot easier. I can read more & remember more details…

Here is my approach:

Read the book once to understand concepts.
Take the practice test to find “holes” in your general understanding.
Read the book twice to remember details.
Take the practice test again to find “holes” in your detail understanding .
Write down “details” you missed.
Focus on reading those details.
Take practice test again.

Passing MCTS Exam 70-536

First look at Nevron chart library

Nevron is comprehensive & very powerful. It is the first OpenGL chart via WinForms (so it has potential of WPF chart, which is DirectX-based). In term of feature set, it has everything Dundas has.  

But it  is also complicate & somewhat confusing & harder to use. Documentation is good but not as good as Dundas.

You have to go down several level to reach a  particular property field; while the same property field in Dundas can be accessed in 1 shot. It took me 1 hour to build a cool/fancy template Dundas chart (diagonal gradient background, emboss style, time-based series, title font/style, legend…). And it took me 2 days to the same task in Nevron with the help you Nevron support team!

After the initial learning curve, things get a lot easier.  I was able to do a lot of things with it and found it very powerful, even more powerful than Dundas with cheaper price. I do like it now.

First look at Nevron chart library

First look at Dundas chart library

I had a chance to evaluate Dundas .NET WinForms chart ((there is no WPF version yet)) in 2008.
Dundas chart is very polish, very easy to use, has great documentation and has a comprehensive feature set: real time charting, scaling, zooming, scale brake, annotations, context menu, threshold (if value goes down/up to a certain value, chart will pop up an annotation! cool!) … It took me 1 hour to build a cool/fancy template Dundas chart (diagonal gradient background, emboss style, time-based series, title font/style, legend…) thank to Dundas chart builder tool. But it is expensive. Enterprise version is not royalty-free! Ooch!
Update:
 –  Here is link to Dundas feature list
– I mentioned chart threshold feature but the feature is not listed in standard feature list. I had to dig it out from demo.  Here is link to Dundas chart threshold feature.
First look at Dundas chart library

Hello World on Windows Mobile

I have HTC Touch cellphone running on Windows Mobile 6.1. I always wanted to write  some apps on it for awhile just to play with it. Finally, I was able to find time and wrote a Hello World app using WinForms.

The cool part of .NET is that I can run the problem on my PC w/o using emulator. In fact, it’s faster to debug by running the .NET assembly then using emulator.

Hello World on Windows Mobile

.NET 3rd party chart evaluation/review

I had a chance to work on chart component (2008). I went through a lot of 3rd party products. I not only researched using their websites but also downloaded the software package, wrote a prototype for each to form opinion about it…
The evaluation took longer than I expect . I think it’s worth it because you don’t want to commit a toolkit and later find out its shortcoming.
I considered both WinForm charts and WPF charts. Eventually, I narrowed down to 4 companies: Dundas, Nevron, Infragistics, SyncFusion… Leave comment if you want to see review of other products such as ComponentOne, DevComponents… I also a comparison spreadsheet. If I found it, I’ll post it.
Here are my first look at those charts:
We decided on WinForm chart because WPF charts currently offering is not mature enough.
Dundas chart wins out as it is very easy to use and has comprehensive feature set.
But later I found out that the feature we need is only available in Enterprise edition. Enterprise edition license is not royalty-free license; making it prohibitively expensive. Management said no.
So we decided on Nevron.
.NET 3rd party chart evaluation/review