Thursday, October 30, 2008

Multithreading and events



Don't you just love Multi-threading, well, I love to see my processor being efficiently used and close to at least 70% utilization. even though threads are dangerous however, using threads carefully will allow your user interface to always be responsive.
Nowthat, we will live with threads, those threads will certainly need to talk to the user interface thread. There are many ways for them to start chit chatting and my prefered way is events.
In this mechanism, a thread will raise an event and anyone who is subscribed to this event will receive the event data and will have a chance to process it.
Here is how to do that.
Let's say you have a calls called "Communicator" which will be processed in a thread and need to raise an event when the thread is done. what you need to do is
1- Define a delegate and an event as follows
//Delegate
public delegate void AlertSentHandler(object sender, SentAlertEventArgs e);
//Event
public event AlertSentHandler AlertSent;
2- Raise the event at the proper time as follows
onAlertSent(this,new SentAlertEventArgs(DateTime.Now.ToString()));
3- Since you are using a helper function called onAlertSent then you need to write it as follows
Protected void onAlertSent(object sender, SentAlertEventArgs e)
{
if (AlertSent != null)
AlertSent(sender, e );
}
The onAlertSent method is checking if the delegate is not null (There are subscribers to the event) and then call the delegate itself as shown above.

Now this event is sent because someone will probably care that the thread is done. those who care that the thread is done should subscribe to the event. To subscribe to this event simply subscribe as follows
cm.AlertSent += new Communicator.AlertSentHandler(cm_AlertSent);
and that is...!!!!!
Note: The above topic is different from serializing the threads (incorrectly called Synchronizing the threads). When you need threads to wait for each others you may want to use the serialization techniques, one of them is the ManualResetEvent
which you may declare like this

static ManualResetEvent threadSerializer = new ManualResetEvent(true );
and make your threads WaitOne like this
threadSerializer.WaitOne();
and set and reset like this
threadSerializer.Reset();
//Do thread work
threadSerializer.Set();
There is a complete and good article about this type of serialization techniques in these links

Wednesday, October 29, 2008

My Notes from "About Face" Book

As I am reading this book called "About Face" I remember David Platt's quote about blindly implementing a customer's requirement in software.
He is saying "Imagine a patient who visits his doctor with a horrible stomachache. "Doctor," he says, "it really hurts. I think it's my appendix. You've got to take it out as soon as possible." Of course, a responsible physician wouldn't perform the surgery without question. The patient can express the symptoms, but it takes the doctor's professional knowledge to make the correct diagnosis."

About Face book is saying

Current software, websites interface routinely
1- Make users feel stupid
2- Cause users to make big mistakes
3- Require too much effort to operate effectively
4- Don't provide an engaging or enjoyable experience

To those who do not have a design step I'd say
"You can not effectively design a house after construction begins"

Tuesday, October 14, 2008

Unhandled Exception

As I am working towards my master in information technology from Harvard university here in Cambridge, I attend a class currently taught by David Platt . The previous week assignment was about exceptions and I didn't know that there were two types of global exceptions that you can catch.

I used to know and use the event

Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(
Application_ThreadException);

I just learned that this is not necessarily going to catch all unhandled exceptions, it will only catch all unhandled exception that occur in the UI thread.

If you really want to handle all unhandled exceptions then you need to broaden your scope and implement the domain unhandled exception event as follows.

AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);

I used to know about both events but didn't really know the difference until I learned about them from my TA (Kevin).

Go to this blog for more details


Have fun catching exceptions.

Thursday, October 09, 2008

Service-Based Database

If you still like those good old days of having your database file shipped with your application instead of relying on a SQL server be installed at the client machine, then you may want to add a SQL Server Express Database to your application. It is still possible to add a file based database to your application which Microsoft calls Service-Based Database [I don't think anyone knows why is it called this] but anyway, you can add it and that's what matters.

You can add the database by right click your project and select add item, then select data from the categories box and then select Service-Based Database as per the screen shot below.


However, if you do so, you may be challenged by the error message

Failed to generate a user instance of SQL Server due to a failure in
starting the process for the user instance. The connection will be
closed.

To solve this problem you really wanna make sure you have an administrator user on your machine. it seems you must start your visual studio as an administrator rather than any other user to be able to add this database to your solution.

To do so and start your visual studio as an administrator, right click your visual studio short cut and select run as as per the screen shot below












When you do so, you will get another dialog box that will ask you to type the name of the administrator user and the password. type the administrator user account and password.



Now don't tell me I am already an administrator on my machine!! doesn't work. the user name has actually to be called "Administrator" if you're an administrator on your machine in the administrators group that doesn't help. you ask why? I don't know.

If your machine does not have an administrator user called Administrator, then create that user and add it to the administrators group so that you can type that name in the dialog above.

Some people reported that this solution worked with them


HTH