[英]Stop execution of a method c#
单击按钮时如何停止执行方法?
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
}
}
当我单击停止按钮时,如何结束我的公共无效foreach 。 当我单击停止而不关闭我的应用程序时,我想停止下载或提取文件等。
我试过使用return,但它会继续执行(下载/解压缩等)。
您不能停止执行单个方法。 您可以通过使用线程来使用它。 使用新线程运行该方法,并在单击按钮时提示该线程停止。 查看此线程-C#线程-如何启动和停止线程 http://msdn.microsoft.com/zh-cn/library/7a2f3ay4(v=vs.90).aspx
停止ForEach()应该很容易,但是停止每个Case定义的每个方法取决于您在这种情况下的工作。
// Defined somewhere which is available to you ForEach() and your Stop button.
// In the stop button set the KeepProcessing to false.
bool KeepProcessing = true;
foreach (string item in Processes)
{
if(!KeepProcessing)
return;
switch(.....
}
快速而肮脏的解决方案可能是对变量进行测试,然后通过按停止按钮设置该变量。 请注意,这仅适用于WinForm应用程序,对于WPF应用程序,则需要导入system.windows.forms.dll-有效地使其成为非wpf。
例如:
bool stop = false;
foreach (string item in Processes)
{
if (stop)
break;
//do-your-stuff
Application.DoEvents(); //this will force the winform app to handle events from the GUI
}
//Add an eventhandler to your stop-button
private void stopButton_Click(System.Object sender, System.EventArgs e)
{
stop = true;
}
但是,正如Rabi所建议的那样,最好在应用程序中引入一些线程。 对于WPF,调度员会创造奇迹。 请参阅http://msdn.microsoft.com/en-us/magazine/cc163328.aspx 。
取消工作线程执行的最规范的技术是一种称为“ 合作取消”的模式。 MSDN上有一篇不错的文章 。 请注意,这与使用基于任务的异步编程有关。
要点类似于以下内容:
public void DoWork(IEnumerable<Work> workItems, ref bool cancel)
{
foreach(thing in workItems)
{
//poll to see if you need to abandon the work.
if(cancel)
throw new WorkCancelledException(); //or whatever exception you want to use. MSDN shows you a built in one.
ProcessThing(thing); //process the item. An item is just a unit of work. You dont have to use a collection of work items.
}
}
按下取消按钮时,设置取消布尔值。 MSDN示例使用CancellationToken
,我会推荐它。 它不限于Task
实现。
为什么要合作? 消费代码和执行代码都必须就取消方法达成一致,并在cancel
为true时同意取消操作。
我不习惯Wpf,但是我认为如果需要使用线程,则使用BackGroundWorker是正确的解决方案。
https://stackoverflow.com/a/5483644/906404
private readonly BackgroundWorker worker = new BackgroundWorker();
// Initialization code
worker.DoWork += worker_DoWork;
worker.WorkerSupportsCancellation = true;
worker.RunWorkerAsync(); // Runs your code in a background thread
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
CreateFolders();
//For each process get the equivalent processcode
foreach (string item in Processes)
{
-->> if (worker.CancellationPending) break;
ItemValue = item;
//Get process code for the process under the selected source
GetActiveProcess();
... your code...
}
}
void OnClickStopButton(args) {
worker.CancelAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.