停止在C#windows应用程序中执行进程

编程入门 行业动态 更新时间:2024-10-21 13:06:29
本文介绍了停止在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应用程序中执行进程

本文发布于:2023-11-08 20:34:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   进程   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!