如何停止Task.Factory [英] How to stop Task.Factory
本文介绍了如何停止Task.Factory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取选定的路径文件和文件夹数量我使用下面的代码
var tCmbGetCount = Task.Factory.StartNew(()= >
{
任务innerCmbGetCount = Task.Factory.StartNew(()= > GetCount());
innerCmbGetCount.Wait();
return innerCmbGetCount;
});
public long GetCount()
{
try
{
// 计算选定的路径文件和文件夹数并分配给标签
lblFolderCount.Text = strFinalFolderCalc.ToString();
lblFileCount.Text = strFinalFileCalc.ToString();
return strFinalFolderCalc;
}
catch (System.Exception ex)
{
return 0 ;
}
}
但我需要在开始前停止任务。
怎么做
解决方案
在下面的代码中使用以下代码取消任务。
innerCmbGetCount。取消();
Hi,
to get selected path files and folder count i have used below code
var tCmbGetCount = Task.Factory.StartNew(() =>
{
Task innerCmbGetCount = Task.Factory.StartNew(() => GetCount());
innerCmbGetCount.Wait();
return innerCmbGetCount;
});
public long GetCount()
{
try
{
//calculate selected path file and folder count and assign to label
lblFolderCount.Text = strFinalFolderCalc.ToString();
lblFileCount.Text = strFinalFileCalc.ToString();
return strFinalFolderCalc;
}
catch (System.Exception ex)
{
return 0;
}
}
but i need to stop task before start.
How to do it
解决方案
Use below code wherever you want to cancel the Task.
innerCmbGetCount.Cancel();
这篇关于如何停止Task.Factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文