进程终止后清理 AppBar

发布于 2024-07-08 21:53:34 字数 181 浏览 3 评论 0原文

我编写了一个应用程序桌面工具栏(又名 AppBar),它工作得很好,除了如果我终止进程,AppBar 代码永远没有机会通过发送 ABM_REMOVE 进行清理。 问题是这基本上会破坏用户的桌面。 AppBar 是使用互操作代码在 .NET 中编写的。

有谁知道清理此资源的方法,即使在任务管理器终止进程的情况下也是如此?

I have written an Application Desktop Toolbar (a.k.a AppBar), it works great except for the fact that if I kill the process, the AppBar code never gets a chance to cleanup by sending an ABM_REMOVE. The problem is that this basically screws the users desktop up. The AppBar is written in .NET using interop code.

Does anyone know of a way to clean this resource up, even in the case of a process kill from TaskManager?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-07-15 21:53:35

当从任务管理器终止进程时,该应用程序内不会引发任何事件。 通常使用单独的帮助程序应用程序来侦听进程的 Win32_ProcessStopTrace 事件。 您可以使用 WqlEventQuery,它是 System.Management 的一部分。

以下是来自 MegaSolutions 帖子

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(); 
    } 
}

When a process is killed from Task Manager, no events are raised within that application. It's common to use a seperate helper application that listens for the Win32_ProcessStopTrace event for your process. You can use the WqlEventQuery, which is part of System.Management for this.

Here is some example code for this from 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(); 
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文