【发布时间】:2014-08-06 09:36:52
【问题描述】:
当我点击一个按钮时,我怎样才能停止我的方法的执行?
public void ForEach()
{
CreateFolders();
//For each process get the equivalent processcode
foreach (string item in Processes)
{
ItemValue = item;
//Get process code for the process under the selected source
GetActiveProcess();
#region Switch
switch (ProcessCode)
{
#region DownloadFile
case "Download File":
ForDownloadFile();
break;
#endregion
#region UnzipFile
case "Unzip File":
ForUnzipFile();
break;
#endregion
#region RenameFile
case "Rename File":
ForRenameFile();
break;
#endregion
#region MergeFile
case "Merge File":
ForMergeFile();
break;
#endregion
#region ZipFile
case "Zip File":
ForZipFile();
break;
#endregion
#region UploadFile
case "Upload File":
ForUploadFile();
break;
#endregion
}
#endregion
}
}
当我单击停止按钮时,如何结束我的 public void foreach。当我点击停止而不关闭我的应用程序时,我想停止下载或提取文件等。
我已尝试使用 return,但它会继续执行(下载/提取等)。
【问题讨论】:
-
您自己尝试过吗?
-
@Dan Puzey,我尝试使用 return 但它继续执行。它继续下载,或提取等
-
您是否在后台线程中运行代码?
-
使用 BackgroundWorker
-
@DanPuzey,我已经通过分配一个布尔取消来解决它,一旦它变为真,它就会返回。我把它放在我所有的方法中(ForZipFile、ForMergeFile 等)