我想在一个额外的线程中执行Heavy Process
之后可以访问result
(重进程的返回结果),因为它主要是期望在进程UI生效期间不需要, 过程完成后会与result
从一个额外的线程返回结果
播放过程中某些搜索,并期待在我的文章中,我得到了有几种方法到 做到这一点,请提供你知道最好的方法或类似的情况下使用
这就是我要处理的最可能的方法的例子:
public ACollection DoProcess(Document docProcess)
{
ACollection result = new ACollection();
ThreadStart threadStart = delegate
{
result = MyProcess(docProcess);
};
var threadProcess = new Thread(threadStart);
threadProcess.Start();
return result ;
}
其他可能的方法可能是IAsyncResult的,BackgroundWorker的,使用定时器和检查状态,而不是将结果返回到方法来处理它,它报告给UI直接在线程安全的方式给我们的UI控件发送...
请给你自己的意见和样品类似的情况下, 在此先感谢
编辑3:方法 - 基于Brian的回答
LenzCollection myResultCollection = new LenzCollection();
TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
myResultCollection = DoCollect(docProcess);
//Task.WaitAll();
return myResultCollection;
}).ContinueWith((task =>
{
myResultCollection = task.Result;
}), ui);
return myResultCollection;
它仍然没有等待,将不会得到预期的结果
'while(threadProcess.IsAlive){}'使这个单线程。还有一个CPU-Hog。 – 2012-04-13 10:20:07
我得到了几分钟后,编辑,谢谢,但仍需要知道你建议的可重复使用的方法,thr – LastBye 2012-04-13 10:22:06
BackgroundWorker已经得到了所有你问到的功能。那么为什么要重新发明轮子? – Zarat 2012-04-13 10:26:47