c# - 如何在程序关闭后运行操作?

标签 c# .net

我是 c# 的新手,我发现它与我的 PHP 起源有很大不同,所以我这样做可能是完全错误的。但是根据我的理解,它非常准确。基本上我需要在我的表单中单击按钮时启动一个程序(该部分完美运行)然后当用户关闭该程序时我需要运行一个操作,在这种情况下我会显示一个消息框。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace usb
{
    class Applications
    {
        //condition variables
        public static bool CalcStatus = false;

        public static void Calc()
        {
            Process CALC;
            CALC = Process.Start("calc.exe");
            if (CALC.HasExited)
            {
                MessageBox.Show("CALC has exited");
                CalcStatus = true;
            }
        }
    }
}

这个片段是一个类的一部分,另一个类使用这个函数,当它被调用时,calc 将打开框,当 calc 关闭时,不会弹出警告。有谁知道我做错了什么?欢迎任何更多的指点,因为我还在学习:)

最佳答案

您使用 Exited 事件在进程退出时运行一些代码:

CALC.EnableRaisingEvents = true;
CALC.Exited += (sender, args) => MessageBox.Show("CALC has exited");

关于c# - 如何在程序关闭后运行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005531/

相关文章:

c# - 如何存储可部署的 C# 应用程序的设置?

c# - 没有直接在类中实现的接口(interface)的实际用法是什么?

c# - 缓存反射数据

c# - 用于 C# 代码的 Pascal 大小写或 Camel 大小写?

java - Opensaml 验证与 ComponentSpace 验证

c# - 从字符串列表中获取不同且有序的成员 - linq 或 hashset for unique 哪个更快/更适合

c# - 当 Controller 在单独的类库中时覆盖 MVC Controller

c# - .Net StreamWriter.BaseStream,这个定义是什么意思? "Gets the underlying stream that interfaces with a backing store."

c# - 当一切都在编译时已知时,为什么我不能将 nameof 与泛型一起使用?

.net - 从.net Data.ToBinary()中提取数据