的MessageQueue和异步/等待 [英] MessageQueue and Async / Await

查看:181
本文介绍了的MessageQueue和异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望在一个异步方法来获得我的信息!其冻结我的UI

I only want to receive my message in a async method! and its freezing my UI

    public async void ProcessMessages()
    {
        MessageQueue MyMessageQueue = new MessageQueue(@".\private$\MyTransactionalQueue");
        MyMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });

        while (true)
        {
            MessageQueueTransaction MessageQueueTransaction = new MessageQueueTransaction();
            MessageQueueTransaction.Begin();

            ContainError = false;
            ProcessPanel.SetWaiting();

            string Body = MyMessageQueue.Receive(MessageQueueTransaction).Body.ToString();

            //Do some process with body string.

            MessageQueueTransaction.Commit();
        }
    }

我打电话是像任何常规方法的方法和它的工作号!
这code使用时,我使用BackgroundWorkers代替异步/的await

I am just calling the method like any regular method and its nos working! This code used to work when i was using BackgroundWorkers instead of async/await

想法?

推荐答案

斯蒂芬写道,异步不会运行在一个线程中的code。幸运的是,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx\">TaskFactory.FromAsync与<一个href=\"http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.beginreceive.aspx\">MessageQueue.BeginReceive/MessageQueue.EndReceive异步接收消息:

As Stephen writes, async doesn't run your code in a thread. Fortunately, you can use TaskFactory.FromAsync with MessageQueue.BeginReceive/MessageQueue.EndReceive to receive messages asynchronously:

    private  async Task<Message> MyAsyncReceive()
    {
        MessageQueue queue=new MessageQueue();
        ...
        var message=await Task.Factory.FromAsync<Message>(
                           queue.BeginReceive(),
                           queue.EndReceive);

        return message;

    }

您应该注意的是,虽然没有使用事务BeginReceive的一个版本。从BeginReceive的文档:

You should note though that there ISN'T a version of BeginReceive that uses a Transaction. From BeginReceive's docs:

不要事务使用异步调用BeginReceive。如果要执行事务异步操作,调用BeginPeek,并把交易和(同步)您为查看操作创建事件处理程序中接收的方法。

Do not use the asynchronous call BeginReceive with transactions. If you want to perform a transactional asynchronous operation, call BeginPeek, and put the transaction and the (synchronous) Receive method within the event handler you create for the peek operation.

这是有道理的,因为不能保证你有多久等待回应或线程将最终处理完成的呼叫。

This makes sense as there is no guarantee how long you have to wait for a response or which thread will eventually handle the completed call.

要使用事务,你会这样写:

To use transactions you would write something like this:

    private  async Task<Message> MyAsyncReceive()
    {
        var queue=new MessageQueue();

        var message=await Task.Factory.FromAsync<Message>(queue.BeginPeek(),queue.EndPeek);

        using (var tx = new MessageQueueTransaction())
        {
            tx.Begin();
            queue.Receive(tx);
            //Process the results inside a transaction
            tx.Commit();
        }
        return message;
    }

这篇关于的MessageQueue和异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆