public void BeforeSendReply(ref Message reply, object correlationState) { var replyCopy = reply; ThreadPool.QueueUserWorkItem(delegate { RequestQueueHandler.RequestQueue.Add((Message)correlationState, replyCopy); }); }
against
private delegate void RequestQueueHandlerAdd(Message request, Message reply); private static void AsyncMethod(Message request, Message reply) { RequestQueueHandler.RequestQueue.Add(request, reply); } public void BeforeSendReply(ref Message reply, object correlationState) { ((RequestQueueHandlerAdd)AsyncMethod).BeginInvoke((Message)correlationState, reply, null, null); }
which of these two should i use? (which works better?) why?
Does my method overhead affect the decision, or is one of these implementations always superior to the other?
for what reason?
I'm prone to ThreadPool.QueueWorkerUserItem, but I have no idea which one is really better, neither in this case, nor at all
UPDATE
I read something about TPL .. did this:
public void BeforeSendReply(ref Message reply, object correlationState) { var replyCopy = reply; var enqueue = Task.Factory.StartNew(() => RequestQueueHandler.RequestQueue.Add((Message)correlationState, replyCopy)); }
how should i handle the exception here? I mean, if I do
public void BeforeSendReply(ref Message reply, object correlationState) { var replyCopy = reply; var enqueue = Task.Factory.StartNew(() => RequestQueueHandler.RequestQueue.Add((Message) correlationState, replyCopy)); **try { enqueue.Wait(); } catch(AggregateException e) { Handle(e); }** }
I did not miss the whole point of parallelism here?
Should I just handle the possible RequestQueueHandler.RequestQueue.Add exception in the RequestQueueHandler.RequestQueue.Add method?