本文介绍了停止在C#windows应用程序中执行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Windows窗体有两个按钮:开始和停止。如何在不关闭整个应用程序的情况下阻止表单执行任何操作? 停止按钮必须停止正在另一个类中运行的进程。 我尝试了什么: 在我的应用程序中我正在使用计时器和线程进行操作
My Windows form has two buttons: "Start" and "Stop". How can I stop the form from doing whatever it is doing without closing the whole application? The "Stop" button would have to stop a process that's being run in another class. What I have tried: in my application i am doing operation using timer and thread
推荐答案这取决于你想要的进程的实现停止,但你通常在它上面标记一些东西,告诉它你要停止它。 It depends on the implementation of the process you want to stop, but you normally flag something on it that tells it you want it to stop. class Program { static void Main(string[] args) { Worker w = new Worker(); Thread t = new Thread(new ThreadStart(w.DoWork)); t.Start(); Console.WriteLine("Press any key to stop"); Console.ReadKey(); Console.WriteLine("Stopping worker..."); w.Stopping = true; t.Join(); Console.WriteLine("Worker stopped"); } } class Worker { public bool Stopping { get; set; } public void DoWork() { while (!Stopping) { System.Diagnostics.Debug.WriteLine (DateTime.Now); System.Threading.Thread.Sleep(1000); } } }
您可以在停止按钮上使用Thread.Stop(),也可以直接调用 Either you can use Thread.Stop() on stop button or you can directly call Environment.Exit(1)
立即停止。
to stop immediately.
更多推荐
停止在C#windows应用程序中执行进程
发布评论