简体   繁体   English

需要全面的C#System.Threading.Tasks示例

[英]Need comprehensive C# System.Threading.Tasks example

I've been trying to figure out how to use System.Threading.Tasks to asynchronously invoke a synchronous WCF method while supporting cancellation, error handling, result-return and multiple continuations. 我一直在试图弄清楚如何使用System.Threading.Tasks异步调用同步WCF方法,同时支持取消,错误处理,结果返回和多个延续。

I've come across a number of incomplete demos but they all seem to fall a bit short. 我遇到了一些不完整的演示,但它们似乎都有点短暂。 As an example I can't use cooperative cancellation since all of my WCF methods are atomic and relatively long lived. 作为一个例子,我不能使用合作取消,因为我的所有WCF方法都是原子的并且相对较长。

I would consider an alternate option - generate the WCF client interface with asynchronous methods. 我会考虑一个替代选项 - 使用异步方法生成WCF客户端接口。 This will allow you to make all WCF service calls asynchronously (from the perspective of the client application), which will allow you to support cancellation, error-handling, result-return, etc. 这将允许您异步地(从客户端应用程序的角度)进行所有WCF服务调用,这将允许您支持取消,错误处理,结果返回等。

The benefit is that you won't be blocking a thread on the worker thread pool (like System.Threading.Tasks will), and you don't have the worry of trying to figure out how to cancel a synchronous WCF call in your worker thread. 好处是您不会阻塞工作线程池中的线程(如System.Threading.Tasks将),并且您不必担心尝试如何取消工作中的同步WCF调用线。

Is there a reason that you must make a synchronous WCF call? 您是否有必要进行同步WCF呼叫?

If you explained what your doing a bit more it might help. 如果你解释了你的做法多一点可能会有所帮助。

However you seem to be saying you want an async version of a syncronous call and this isn't what Tasks is about. 但是你似乎在说你想要一个同步调用的异步版本,这不是任务的内容。

Task based processing is about making an async call look like a syncronous call. 基于任务的处理是关于使异步调用看起来像同步调用。 Not act like it just the code look like thats the way it works. 不像它只是代码看起来像它的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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