I’am working on an UDP listener windows application, once an UDP message reach the application i create a new dedicated thread which do a routine relatred to this udp message.
This routine is called asynchrnously.
Why asynch ?
mainly to keep the order in wich the UDP are coming , since asynchronous call are queued in threadpool (not the case if i let each thread run synch).
My questions :
- Is it a good idea to run a routine asynchronously even this routine run in a child thread ?
- if yes, shall i implement IsBusy best pratice for this asynch call, even it’s a fire and forget pattern ?
I hope i am explianing well what i wanna achieve
Apologize for my bad english
Regards
xPridex
N.B : this not the accurate code i have delete lot of details with regard to lisibity.
/// <summary>
/// Launch SendNotificationToEBSECW treatment.
/// </summary>
/// <param name="sender">object : UDPmsg_t_Mapping</param>
private void StartPrepared(object sender)
{
mainThread = new Thread(new ParameterizedThreadStart(EntryPointV3));
mainThread.Start(sender);
}
private readonly object _sync = new object();
private bool _myTaskIsRunning = false;
public bool IsBusy
{
get { return _myTaskIsRunning; }
}
public void DoWorkAsynch(Tuple.Create(x,y))
{
MyTaskWorkerDelegate worker = new MyTaskWorkerDelegate(EntryPointV3);
AsyncCallback completedCallback = new AsyncCallback(MyTaskCompletedCallback);
lock (_sync)
{
if (_myTaskIsRunning)
throw new InvalidOperationException("currently busy.");
AsyncOperation async = AsyncOperationManager.CreateOperation(null);
worker.BeginInvoke(Tuple.Create(x,y), completedCallback, async);
_myTaskIsRunning = true;
}
}
private void MyTaskCompletedCallback(IAsyncResult ar)
{
// get the original worker delegate and the AsyncOperation instance
MyTaskWorkerDelegate worker =
(MyTaskWorkerDelegate)((AsyncResult)ar).AsyncDelegate;
AsyncOperation async = (AsyncOperation)ar.AsyncState;
// finish the asynchronous operation
worker.EndInvoke(ar);
// clear the running task flag
lock (_sync)
{
_myTaskIsRunning = false;
}
// raise the completed event
AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null,
false, null);
async.PostOperationCompleted(
delegate(object e) { OnMyTaskCompleted((AsyncCompletedEventArgs)e); },
completedArgs);
}
public event AsyncCompletedEventHandler MyTaskCompleted;
protected virtual void OnMyTaskCompleted(AsyncCompletedEventArgs e)
{
if (MyTaskCompleted != null)
MyTaskCompleted(this, e);
}
There is nothing obviously wrong with that code, but I do have a few comments:
new Threaddoes not use the thread pool, it always creates a new dedicated thread.If you must handle the messages in the order they are received, you should not handle each message in a separate thread. Instead, add each message to a queue and process each item in the queue sequentially in a background thread.
Threads do not have any built-in way of alerting their creator when they are done, or returning results. If you want to do that, use
System.MulticastDelegate.BeginInvoke; that uses the thread pool. If you want intermediate results while the thread runs, useBackgroundWorker.