【发布时间】:2014-09-26 17:20:21
【问题描述】:
我有一个应用程序正在使用 .net 4.0 中的任务处理 FIFO 队列中的项目。
我是 .net 中的 TPL 和 Tasks 的新手,想知道是否有一个简单的解决方案可以解决我的问题:
Task 中的 Action 委托被分配给在异步套接字上发送和接收数据的方法。我遇到的问题是任务“过早地”结束。如何告诉任务等到所有通信完成后再处理队列中的下一个项目?
一种解决方案是切换到使用同步套接字,但我希望有一种方法可以使用异步套接字来做到这一点。
编辑 添加了一些代码:
class Program
{
private BlockingCollection<string> myQueue;
private CancellationTokenSource cancellationSignalForConsumeTask;
private CancellationTokenSource cancellationSignalForProcessCommandTask;
private AsyncSocket mySocket;
public void Main(string[] args)
{
mySocket = new mySocket();
myscoket.ReceiveData += mySocket_ReceiveData;
cancellationSignalForConsumeTask = new CancellationTokenSource();
Task listenerTask = Task.Factory.StartNew((obj) => Consume(),
cancellationSignalForConsumeTask.Token,
TaskCreationOptions.LongRunning);
while (true)
{}
}
private void Consume()
{
while (!myQueue.IsCompleted )
{
string _item = myQueue.Take();
cancellationSignalForProcessCommandTask = new CancellationTokenSource();
Task t = new Task(() =>
{
cancellationSignalForProcessCommandTask.Token.ThrowIfCancellationRequested();
DoSomeWork(_item);
}, cancellationSignalForProcessCommandTask.Token, TaskCreationOptions.LongRunning);
t.Start();
t.Wait();
}
}
private void DoSomeWork(string _item)
{
mySocket.SendData("Data to server that could take a long time to process")
}
private void mySocket_ReceiveData(string dataFromServer)
{
string returnMessage = dataFromServer;
//I want the Task to end here...
}
}
问题是任务在DoSomeWork() 方法完成时结束(我明白为什么),有没有办法可以手动告诉任务通过CancellationTokenSource 对象结束?
【问题讨论】:
-
可能是根线程/任务上的 Wait()。你能展示一下你目前拥有的代码吗?
-
发布你目前所拥有的。
-
AsyncSocket是什么?如果SendData()是异步的,那么你可能需要await它。
标签: c# .net multithreading task-parallel-library task