|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Multi threading- The Killer tool.NET threading is a powerful tool that can be used by applications that demand high scalability. Without using In .NET, the perfect OO platform threads are also created and accessed as objects. The main and multiple worker threads model, is used by many applications. In this model a parent thread starts many worker threads based on the service required. All the worker threads do the same work but within a pre-defined context set by the parent. E.g. In a database server application a new worker thread is created whenever a new client is connected. This is an over simplified example and the actual logic is more complex than this.
In this model the worker thread may want to send messages to the main thread to report its progress. The crucial part in this messaging is that the message notifying code should be executed by the Parent thread and not by the worker thread. This project can be used as a tool to accomplish the above mentioned. This is a Thread pool management system with communication facility enabled. There is a class called The class implements an public interface IThreadManager
{
int ThreadCount
{
get;
}
void SetRunMethod(MethodNoParam Run);
void SetMessageReceiveMethod(MethodWithParam Receive);
void Start();
void Stop();
void SendMessageToMain(object Message);
}
// Specify no of worker threads to start
_ThreadMgr = new ThreadManager(Convert.ToInt32
(ThreadCount.Value.ToString()));
MethodNoParam _RunMethod = new
MethodNoParam(this.ThreadMehod);
MethodWithParam _ReceiveMethod = new
MethodWithParam(this.CallBackMethod);
// Code executed by Worker threads
_ThreadMgr.SetRunMethod(_RunMethod);
// Message receiving method- Main thread
_ThreadMgr.SetMessageReceiveMethod(_ReceiveMethod);
_ThreadMgr.Start();
When the worker threads started, they increment the private void ThreadMehod()
{
try
{
while (true)
{
Monitor.Enter(this);
if (RecordsProcessed >= RecordsToBeProcessed)
{
Monitor.Exit(this);
break;
}
else
{
RecordsProcessed +=20;
Monitor.Exit(this);
_ThreadMgr.SendMessageToMain(RecordsProcessed);
Thread.Sleep(500);
}
}
}
catch
{
}
}
The main thread extracts the data passed by the worker threads. In this case it updates the progress bar. private void CallBackMethod(object State)
{
this.Invoke(_ShowCurrentState,new object[]{State});
//System.Diagnostics.Debug.WriteLine
// (Thread.CurrentThread.Name);
}
The thread which executes this code can be confirmed as Main by un-commenting the second line. The For other design patterns such as pipeline model where one thread is dependent on the other, we can use the built in functions such as Following shows the public void DisptachMessage()
{
while(m_Started)
{
Monitor.Enter(this);
while(m_Messages.Count > 0)
{
m_MessageReceiveMethod(m_Messages.Dequeue());
}
Monitor.Exit(this);
Thread.Sleep(100);
}
}
As the private void CallBackMethod(object State)
{
this.Invoke(_ShowCurrentState,new object[]{State});
}
The message that is received by the
We can use delegate's References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||