我正在使用VS 2010,winForm。
当程序进程结束时,有人可以建议如何运行方法吗?
我试过了:How to run code before program exit? 解决方案,但它只能通过“关闭按钮”关闭表单如果我在任务管理器中结束进程它没有被调用,是否可以处理这样的事件?
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnProcessExit);
}
public static void OnProcessExit(object sender, EventArgs e)
{
if (Utils.RemindTime != DateTime.MinValue)
Utils.SetStartup(true);
}
答案 0 :(得分:1)
您使用其他程序启动此程序。 假设您的应用程序是“记事本”:
class Program
{
static void Main(string[] args)
{
var startInfo = new ProcessStartInfo("notepad.exe");
var process = Process.Start(startInfo);
process.WaitForExit();
//Do whatever you need to do here
Console.Write("Notepad Closed");
Console.ReadLine();
}
}
您可以使用类似于Windows上的批处理文件 或Linux中的shell脚本。
当然,它不允许您访问您启动的程序的内部, 但这是唯一可以确保在任务管理器终止进程后执行“某些代码”的方法