如何停止c#应用程序的当前进程并启动另一个函数
本文关键字:进程 启动 另一个 函数 何停止 应用程序 | 更新日期: 2023-09-27 18:10:22
我正在解释我的场景,我有一个函数,打印1到10000,而打印我必须停止进程,让用户知道当前的I值,如果用户按下回车键,它应该再次继续。
我使用
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();
}
}
或者你可以使用定时器....