Hi fellow Code Project coders
I have a WCF service, consumed by a Silverlight application that, for testing purposes, handles callbacks and a
CallbackChannel
client list for a small testing chat application.
I was running into some problems in the main application I’m working on and mighty Google told me that I needed to implement
multithreading
.
Ok, multithreading it is! Let’s test it.
After some changes on my service I’ve got a stable solution working on my developing environment that goes like this:
[OperationContract]
public void Publish(string message)
{
lock (clients)
{
foreach (IDBNotificationCallbackContract channel in clients)
{
Thread t = new Thread(new ParameterizedThreadStart(this.notifyClient));
t.Start(new Callback_info { CallBackContract = channel, Message = DateTime.Now.ToString() + "\n" + message });
}
}
}
void notifyClient(Object n)
{
Callback_info notif = (Callback_info)n;
try
{
if ((notif.CallBackContract as ICommunicationObject).State == CommunicationState.Opened)
notif.CallBackContract.Notify(notif.Message);
}
catch
{
}
}
Although the
thread
initialization is made inside the
foreach
cycle, I wondered if it really gets
disposed
after the thread is completed or am I running into memory and/or processor load troubles if this ever goes to production like this?
If so, how do I dispose the thread? There is no Dispose method in the Thread class neither there is a Completed event to hook up to asynchronously.
The thing is that on my development environment I tried a small stress test and opened 40 browser windows running the test application.
When pressed the button to start bombing messages the machine went nuts for 20 minutes with a 100% load during that time and I’m not sure that the load was from the service or the 40 opened browser windows.
Appreciate any light on this doubt of mine.