【发布时间】:2017-02-09 07:34:46
【问题描述】:
这是我的故事:我有 wcf 服务。它接收有工作要做的请求。每个任务都插入阻塞队列。服务器将定期从此队列中获取项目并完成工作(在不同的线程中完全异步)。在我的“执行”服务中,我需要知道“我的”任务何时完成。像这样:
public bool Do(int input)
{
// 1. Add task to the BlockingCollection queue
// 2. Block this thread from returning and observe/wait til my task is finished
return true;
}
这是我的建议/解决方案:
public bool Do(int input)
{
// 1. Create a ManualResetEvent object
// 2. Add this object to task
// 3. Add task to the BlockingCollection queue
// 4. Block this thread from returning - wait for ManualResetEvent object
return true;
}
因此,ManualResetEvent 对象的数量将与要执行的任务一样多。我将真正拥有一组同步对象。这对我的问题有很好的解决方案吗?
或者在我的情况下有更好的同步类吗?像等待和脉搏?
感谢您的帮助,
我很抱歉标题。我不知道如何在标题中提出这个问题。
【问题讨论】:
-
为每个任务创建一个类State对象并创建一个List
。将 time 和 id 等属性放入 State 以及线程对象中。
标签: c# .net multithreading wcf synchronization