WCF:在服务器上生成异步工作线程是否安全? [英] WCF: Is it safe to spawn an asynchronous worker thread on the server?

查看:51
本文介绍了WCF:在服务器上生成异步工作线程是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务方法,我想异步执行一些操作(这样在返回到调用方时几乎没有额外的延迟).在方法内生成System.ComponentModel.BackgroundWorker是否安全?我实际上会使用它来调用其他服务方法之一,因此,如果有一种方法可以异步调用其中之一,那将奏效.

I have a WCF service method that I want to perform some action asynchronously (so that there's little extra delay in returning to the caller). Is it safe to spawn a System.ComponentModel.BackgroundWorker within the method? I'd actually be using it to call one of the other service methods, so if there were a way to call one of them asynchronously, that would work to.

BackgroundWorker是前进的方式吗,还是在WCF服务中有更好的方式或这样做的问题?

Is a BackgroundWorker the way to go, or is there a better way or a problem with doing that in a WCF service?

推荐答案

BackgroundWorker实际上更适合在UI中使用.在服务器上,您应该考虑使用ThreadPool.

BackgroundWorker is really more for use within a UI. On the server you should look into using a ThreadPool instead.

when-to-use-thread-pool-in-c 对何时使用线程池有很好的记录.本质上,出于多种原因,在服务器上处理请求时,通常最好使用线程池.例如,随着时间的流逝,您将不会产生创建新线程的额外开销,并且池在任何给定时间都会限制活动线程的总数,这有助于节省负载下的系统资源.

when-to-use-thread-pool-in-c has a good write-up on when to use thread pools. Essentially, when handling requests on a server it is generally better to use a thread pool for many reasons. For example, over time you will not incur the extra overhead of creating new threads, and the pool places a limit on the total number of active threads at any given time, which helps conserve system resources while under load.

BackgroundWorker .例如, System.ComponentModel.BackgroundWorker 的MSDN页面专门涉及UI用例:

Generally BackgroundWorker is discussed when a background task needs to be performed by a GUI application. For example, the MSDN page for System.ComponentModel.BackgroundWorker specifically refers to a UI use case:

BackgroundWorker类允许您在单独的专用线程上运行操作.下载和数据库事务之类的耗时操作可能会使您的用户界面(UI)看起来好像在运行时已停止响应.当您需要响应式UI并面临与此类操作相关的长时间延迟时,BackgroundWorker类提供了一种便捷的解决方案.

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

这并不是说不能在服务器端使用它,而是该类的目的是在UI中使用.

That is not to say that it could not be used server-side, but the intent of the class is for use within a UI.

这篇关于WCF:在服务器上生成异步工作线程是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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