异步开始同样的方法/结束 [英] Asynchronous begin/end in same method

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

问题描述

(我使用.NET 4.0)

(I'm using .Net 4.0)

我要异步调用WCF服务,从我的服务层。这种服务层所使用的和MVC.Net控制器。我读过,这是很好的做法异步调用WCF服务。所以我使用了开始/结束(APM)。我想如果我richt做仔细检查:

I want to call a WCF service asynchronously, from my service layer. This service layer is used by and MVC.Net controller. I've read that it's good practice to call a WCF service asynchronously. So I'm using begin/end (apm). I want to double check if I'm doing it richt:

public byte[] GetSomeData()
{
    IAsyncResult result = myServiceClient.BeginDoSomething(someInputValue, null, null);

    var data = _pdfCreatieService.EndCreateForPreview(result);

    return data;
}

我不完全肯定高于code,因为我已经看到类似下面的code,这似乎在我的情况稍微复杂和不必要的结构:

I'm not totally sure about the code above, because I've seen constructions like the code below, which seem a bit more complex and unnecessary in my case:

public byte[] GetSomeData()
{
    var myState = new MyState();

    IAsyncResult result = _myServiceClient.BeginDoSomething(someInputValue, CreateForPreviewCallback, myState);

    result.AsyncWaitHandle.WaitOne();

    return myState.Bytes;
}

private void DoSomethingCallback(IAsyncResult result)
{
    var myState = (MyState)result.AsyncState;

    myState.Bytes = _myServiceClient.EndDoSomething(result);
}

由于阿夫纳沙哈尔 - 卡斯坦,斯内德和斯托扬诺夫Noseratio。您的回答真的insightfull!

Thanks Avner Shahar-Kashtan, Ned Stoyanov and Noseratio. Your answers are really insightfull!

推荐答案

你的code将要做的是,实际上,采取异步方法并调用它的同步的。当你调用 EndDoSomething 方法,有效阻止您的线程,直到异步方法完成,而这正是一个异步调用的对立面。

What your code will do is, in effect, take an asynchronous method and call it synchronously. When you call the EndDoSomething method, you are effectively blocking your thread until the asynchronous method has completed, which is exactly the opposite of an async call.

当然,你的第二个code座的的调用code同步,通过阻断执行明确,使用WaitHandle中。

Of course, your second code block also calls the code synchronously, by blocking executing explicitly, using the waithandle.

你想要做什么,而不是返回字节[] 从您最初的方法,有你的 DoSomethingCallback 做的事字节主动 - 无论它们存储在可以由控制器来查了一些类成员,引发事件,或做其他事。如果你正在等待你的异步调用,你没有得到任何好处。

What you want to do is, instead of returning the byte[] from your initial method, have your DoSomethingCallback do something active with the bytes - either store them in some class member that can be checked by the controller, raise an event, or do something else. If you're waiting for your async call, you're not getting any benefit.

什么你也可以做,如果你正在使用.NET 4.5或更高版本(或.NET 4.0的VS2012,使用的 BCL异步包)是使用 异步/的await ,这是一个很好的包装,让您巩固调用方法和回调方法到一个单一的,更一致的方法。

What you can also do if you're using .NET 4.5 or higher (or .NET 4.0 in VS2012, using the BCL Async Package) is to use async/await, which is a nice wrapper which will allow you to consolidate the calling method and the callback method into a single, more coherent method.

但不管语法或您选择的库,你的第一步是了解异步编程必然伤了你的code的控制流进调用和结果的回调,或连续的的异步操作。

But regardless of the syntax or libraries you choose, your first step is to understand that async programming necessarily breaks your code's control flow into the invocation and the result callback, or continuation of the asynchronous operation.

这篇关于异步开始同样的方法/结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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