Web 服务中的多线程 [英] Multi-Threading in web service

查看:40
本文介绍了Web 服务中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Web 服务中创建一个具有多个线程的函数,一个向用户返回一个值,一个继续处理事务?我以前从未使用过任何多线程,似乎我也可以通过异步运行事务来实现.还没有写代码.在我开始编码之前尝试看看是否有可能.

Is it possible to created a function in a web service to have multiple threads, one which returns a value to the user, and one that continues to process a transaction? I've never used any multithreading before and it seems like I would be able too by running the transaction Asynchronously. No code has been written yet. Trying to see if it is possible before I begin coding.

推荐答案

尽管您可以触发线程等,但与任何其他 .NET 应用程序非常相似,您必须考虑您的操作环境.

Although you CAN fire off threads and the like, much like any other .NET application you have to take into account your operating environment.

任何长时间运行的进程都应该真正移交给另一个进程,例如 Windows 服务,因为 Web 服务器工作进程可以并且确实会被回收,并且您的线程也会随之而去.您还占用了可用于进一步向 Web 应用程序提供服务请求的线程.

Any long running process should really be handed off to another process like a Windows service as web server worker processes can and do get recycled and with it goes your threads. You're also tying up threads that could be used to further service requests to the web application.

关于 Paul Abbot 所说的,原则上如果您有办法将某种工作 ID 返回给客户端,您可以在另一个请求中轮询处理状态,这可以应用于同一进程内或外外部服务中的进程.

In regards to what Paul Abbot said, in principle if you had a way of returning some kind of work ID to the client you could poll the status of the processing in another request, and this can apply within the same process or out of process in an external service.

这篇关于Web 服务中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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