我试图在用户关闭会话时在程序中执行一些操作。
这是代码:
using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Threading;
public class MyProgram
{
static Process myProcess = null;
public MyProgram()
{
}
// Entry point
static void Main(string[] args)
{
SystemEvents.SessionEnding += SessionEndingEvent; // Does not trigger inmediately, only fires after "myProcess" gets closed/killed
myProcess = CreateProcess("notepad.exe", null);
myProcess.Exited += pr_Exited; // Invoked at "myProcess" close (works ok)
try
{
myProcess.Start();
}
catch (Exception e2) { MessageBox.Show(e2.ToString()); }
System.Windows.Forms.Application.Run(); // Aplication loop
}
static void SessionEndingEvent(object sender, EventArgs e)
{
MessageBox.Show("Session ending fired!");
}
static void pr_Exited(object sender, EventArgs e)
{
MessageBox.Show("Process Closed");
}
static Process CreateProcess(String path, String WorkingDirPath)
{
Process proceso = new Process();
proceso.StartInfo.FileName = path;
proceso.StartInfo.WorkingDirectory = WorkingDirPath;
proceso.EnableRaisingEvents = true;
return proceso;
}
}
我打开我的应用程序,它打开一个记事本。
当我关闭会话时:
换句话说,我的程序仅在我启动的进程未运行时收到通知。我希望在任何情况下都能调用我的事件,无论流程是否开放。
提前致谢。
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)