简体   繁体   中英

Delegate.BeginInvoke vs ThreadPool.QueueWorkerUserItem

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        var replyCopy = reply;

        ThreadPool.QueueUserWorkItem(delegate
        {
            RequestQueueHandler.RequestQueue.Add((Message)correlationState, replyCopy);
        });
    }

vs

    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 performs better?) why?
does the overhead of my method influence the decision or does one of these implementation always outperform the other?
for what reason?

I'm inclined towards ThreadPool.QueueWorkerUserItem but I have no clue which one is actually better, neither in this case nor in general

UPDATE

I read some stuff about TPL.. worked this out:

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        var replyCopy = reply;
        var enqueue = Task.Factory.StartNew(() => RequestQueueHandler.RequestQueue.Add((Message)correlationState, replyCopy));

    }

how am I supposed to 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);
        }**
    }

Am I not missing the whole point of parallelism here?

Shouldn't I just handle the possible exception throw in the RequestQueueHandler.RequestQueue.Add method?

The Delegate.BeginInvoke() method also uses the ThreadPool, so don't expect any meaningful difference in performance.

QueueUserWorkItem() isn't directly better, just easier in most cases.

But note that both samples are missing Error handling.
Your nice short delegate needs a try/catch, the BeginInvoke scenario a Callback.

So when you can use Fx4 you ought to use the TPL for a much higher abstraction level.

Asynchronuous delegates give you a bit more: return values and exception forwarding (you should call EndInvoke to get access to them). By using ThreadPool directly you have to take care of that yourself.

ThreadPool's advantage on the other hand is simplicity.

Do have a look at this excellent online book , which discusses the two (and more) approaches in depth.

As a rule of a thumb:

  • use TPL if you can
  • if not use ThreadPool directly for simple fire-and-forget tasks
  • if not use async delegates

ThreadPool.QueueWorkerUserItem is higher level and preferred. But ThreadPool.QueueWorkerUserItem behind the scene uses Delegate.BeginInvoke . But Delegate.BeginInvoke uses threads from the ThreadPool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM