Knight’s Tour problem solver in Silverlight

This is a port of my old Knight’s Tour C++ program to Silverlight.
Here is the link

Advertisements
Knight’s Tour problem solver in Silverlight

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

WinForms and WPF interop issue

I had to add  Windows Forms chart control to a WPF app. I hosted it using WindowsFormsHost. Things worked fine until I noticed that there is no scroll bar when I add several controls.

First, I tried to add WPF scrollbar to parent panel of WindowsFormsHost but it behaved weirdly. The scrolled WinForms controls goes out of window bound to other windows! And it is drawn on top other windows. In other words, when scrolled, it floated freely as if there is no window. 

Later, I tried to add a WinForms panel (any type of panel will work) to WindowsFormsHost and add scroll bar to the WinForms panel. It worked! Yeah!

I realized that if you need to scroll WinForms controls, you need to use WinForms scroll bar.

Note that WPF scroll bar is outside of WindowsFormsHost and the WinForms control is inside of WindowsFormsHost.  Using WPF scroll bar to scroll WinForms control is nothing short of begging for trouble.

WinForms and WPF interop issue

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