【发布时间】:2013-03-10 00:18:37
【问题描述】:
请看下面的代码sn-p。我正在尝试执行一个长时间运行的任务,但我不想等待超过给定的超时时间。我想完全控制任务何时开始,因此产生一个新线程并完成工作,并在父线程中等待它。该模式确实有效,但父线程只是在等待。理想情况下,我不喜欢线程休眠/等待,除非它真的需要。我怎样才能做到这一点?欢迎任何建议/想法/模式。
/// <summary>
/// tries to execute a long running task
/// if the task is not completed in specified time, its deemed un-sccessful.
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
bool Task(int timeout)
{
bool workCompletedSuccessfully = false;
//I am intentionally spawning thread as i want to have control when the thread start
//so not using thread pool threads.
Thread t = new Thread(() =>
{
//executes some long running task
//handles all the error conditions
//ExecuteTask();
workCompletedSuccessfully = true;
});
t.Start();
//cannot wait more "timeout"
//My main thread (parent) thread simply waiting for the spawened thread to join
//HOW CAN I AVOID THIS?ANY PATTERN TO AVOID THIS REALLY HELPS?
t.Join(timeout);
if (!workCompletedSuccessfully)
{
//deeemed un-successful
//do the remediation by gracefully disposing the thread
//itnentionally hidden details about disposing thread etc, to concentrate on
//the question - AVOIDING PARENT THREAD TO WAIT
}
return workCompletedSuccessfully;
}
问候, 梦想家
【问题讨论】:
-
确切地说,您希望如何优雅地处理尚未完成工作的线程?
-
无论如何,我不确定您是否可以让您的父线程做一些有意义的事情,并以任何直接的方式接收超时通知。 (也就是说,不让父线程通过可以接收“任务成功”或“任务超时”事件的事件循环工作。)既然,那么,那将如何工作?父线程是否应该被中断并处理通知?
-
@I4V 好主意,但显然需要“异步”整个代码库。 (一个不平凡的改变,但在这里可能是正确的选择。)
-
@millimoose 是的,它要求切换到 TPL 库,但 async/await 不是必须的。
标签: c# .net multithreading