繁体   English   中英

如何停止C#应用程序的当前进程并启动另一个功能

[英]how to stop current process of C# application and start another function

我正在解释我的情况,我有一个功能,在打印时必须打印1到10000,我必须停止该过程,并让用户知道当前的i值,如果用户按Enter,则应再次继续。

我在用

if ((Console.KeyAvailable) && (Console.ReadKey(true).Key == ConsoleKey.Escape))

但这使我正在使用线程的任务复杂化了,即使我设法打破了这个线程,子线程也开始执行,但我只是想暂时停止整个执行过程并执行另一个功能,这是行不通的。

查看BackgroundWorker ,特别是如何实现取消。

基本上,您需要在循环内检查取消是否有待处理。 如果是,则退出循环。

如果您使用线程。 您可以使用这种代码。

// Start
thread = new Thread(new ThreadStart(YourCommand));
thread.Start();

// Pause
if (thread != null)
{
thread.Suspend();
}

//Continue
if (thread != null)
{
thread.Resume();
}

//Alive
if (thread != null)
{
    if (thread.IsAlive)
    {
     thread.Abort();
    }    
}

或者您可以使用计时器...。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM