【问题标题】:Simulate Fire & Forget scenario using C# Threading使用 C# 线程模拟 Fire & Forget 场景
【发布时间】:2013-07-31 09:02:29
【问题描述】:

我有一个 Web 服务,它需要很长时间才能执行。我打算将处理任务委托给后台线程并立即将确认响应返回给用户。

在这种情况下,我担心后台线程的生命周期。
后台线程会在主方法/线程完成执行之前完成委托任务吗?

【问题讨论】:

  • "web 服务方法必须返回值" ,哪个值?后台线程的值?你说你的后台线程会运行一个大任务,为什么要等待它完成?
  • 或者,只是实现一个异步调用,更简单的IMO

标签: c# multithreading wcf isoneway


【解决方案1】:

您应该异步调用您的服务。

var service = new YouServiceClient();
service.SomeMethodCompleted +=
  (sender, args) +=
  {
    //  Put code here to handle the response and extract a return value or whatever.
  }
service.SomeMethodAsync();

详情请参阅How to: Call WCF Service Operations Asynchronously

【讨论】:

    【解决方案2】:

    看起来如果应用程序池被回收,您的后台线程可能会中止。看看这篇文章ThreadPool.QueueUserWorkItem in Web Service for “Fire and Forget” task

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-16
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2021-10-12
      • 2017-02-19
      • 1970-01-01
      相关资源
      最近更新 更多