4
我已经编写了一个应用程序桌面工具栏(a.k.a AppBar),除非事实发生,如果我终止进程,AppBar代码将永远无法通过发送ABM_REMOVE进行清理。问题在于,这基本上将用户桌面搞砸了。 AppBar是使用互操作代码以.NET编写的。删除进程后清理AppBar
有没有人知道一种方法来清理这个资源,即使在从TaskManager中进程kill的情况下?
我已经编写了一个应用程序桌面工具栏(a.k.a AppBar),除非事实发生,如果我终止进程,AppBar代码将永远无法通过发送ABM_REMOVE进行清理。问题在于,这基本上将用户桌面搞砸了。 AppBar是使用互操作代码以.NET编写的。删除进程后清理AppBar
有没有人知道一种方法来清理这个资源,即使在从TaskManager中进程kill的情况下?
当进程从任务管理器中被终止时,该应用程序中不会引发任何事件。通常使用独立的帮助应用程序来侦听您的进程的Win32_ProcessStopTrace事件。您可以使用WqlEventQuery,这是System.Management的一部分。
这是a MegaSolutions post的一些示例代码。
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
class ProcessObserver : IDisposable
{
ManagementEventWatcher m_processStartEvent = null;
ManagementEventWatcher m_processStopEvent = null;
public ProcessObserver(string processName, EventArrivedEventHandler onStart, EventArrivedEventHandler onStop)
{
WqlEventQuery startQuery = new WqlEventQuery("Win32_ProcessStartTrace", String.Format("ProcessName='{0}'", processName));
m_processStartEvent = new ManagementEventWatcher(startQuery);
WqlEventQuery stopQuery = new WqlEventQuery("Win32_ProcessStopTrace", String.Format("ProcessName='{0}'", processName));
m_processStopEvent = new ManagementEventWatcher(stopQuery);
if (onStart != null)
m_processStartEvent.EventArrived += onStart;
if (onStop != null)
m_processStopEvent.EventArrived += onStop;
}
public void Start()
{
m_processStartEvent.Start();
m_processStopEvent.Start();
}
public void Dispose()
{
m_processStartEvent.Dispose();
m_processStopEvent.Dispose();
}
}