Replacing Threads (not ThreadPool Thread) with Asynchronous Delegates (Callbacks).
My Scenario: Spawn a Thread/del.beginInvoke() per Client.
According to me,
Reasons
- need for Notification via Callback / Call delegate again in Callback
- Avoid Thread Overhead, (delegates use Threadpool thread)
- Passing Arguments (Avoid Casting to object) and need return value from the method.
Correct me if above reasons are wrong.
- Is any other Reasons?
- What scenario i exactly need to do some stuff with Asynchronous Delegates that
threads can't?
3.Performance ?
Example
public delegate void SendCallbackType();
SendCallbackType senderdel= new SendCallbackType(SendData);
public void StartSend() // This method Could be Called more than 700 times (Thread per Client)
{
senderdel.BeginInvoke(SendCallback,null);
// (or)
Thread t = new Thread(new ThreadStart(ThreadSend));
t.IsBackground = true;
t.Start();
}
//Async Delegate
void SendData()
{
string data = QueData.DeQueue();
RaiseOnData(data); // Raise to event.
}
void SendCallback(IAsyncResult ar)
{
senderdel.BeginInvoke(SendCallback, null);
}
//Thread
void ThreadSend()
{
while (true)
{
string data = QueData.DeQueue();
RaiseOnData(data); // Raise to event.
}
}
From the above which option would be the best. Performance ?