以c#窗口形式处理托管资源时出现问题

以c#窗口形式处理托管资源时出现问题,c#,C#,在下面的代码中,我试图引用一个外部.dll,它在桌面上创建一个自定义任务栏。之后,我将根据自己的要求在任务栏上创建选项卡 一切正常,但在我终止创建任务栏的应用程序后,自定义任务栏占用的空间被阻止。这意味着,退出应用程序后不会释放资源 我正在尝试强制应用程序处置非托管资源。但这没有帮助。 怎么做??请参考下面我正在尝试的代码 命名空间守护进程 { }appbar被ApplicationDesktopToolbar的OnClose事件删除 您是否正在使用应用程序。退出退出应用程序?发件人: Appl

在下面的代码中,我试图引用一个外部.dll,它在桌面上创建一个自定义任务栏。之后,我将根据自己的要求在任务栏上创建选项卡

一切正常,但在我终止创建任务栏的应用程序后,自定义任务栏占用的空间被阻止。这意味着,退出应用程序后不会释放资源

我正在尝试强制应用程序处置非托管资源。但这没有帮助。 怎么做??请参考下面我正在尝试的代码

命名空间守护进程 {

}appbar被
ApplicationDesktopToolbar
OnClose
事件删除

您是否正在使用
应用程序。退出
退出应用程序?发件人:

Application.Exit
方法不会引发
Form.Closed
Form.Closing
事件[…]

appbar被
ApplicationDesktopToolbar
onClose
事件删除

您是否正在使用
应用程序。退出
退出应用程序?发件人:

Application.Exit
方法不会引发
Form.Closed
Form.Closing
事件[…]


这是一个在后台运行的基于windows的应用程序。因此,如果用户想要退出应用程序,他将从taskmanager中删除.exe。是的,问题与我的类似,但没有提供解决方案。Orelse,如果我对应用程序进行全局处理会更好。我相信如果应用程序从任务管理器中被杀死,那么几乎不可能运行任何清理代码。。。Vista边栏不会显示在任务管理器的“进程”选项卡中;好吧,它是作为Windows服务运行的。您是否考虑过将应用程序转换为服务?这是一个在后台运行的基于windows的应用程序。因此,如果用户想要退出应用程序,他将从taskmanager中删除.exe。是的,问题与我的类似,但没有提供解决方案。Orelse,如果我对应用程序进行全局处理会更好。我相信如果应用程序从任务管理器中被杀死,那么几乎不可能运行任何清理代码。。。Vista边栏不会显示在任务管理器的“进程”选项卡中;好吧,它是作为Windows服务运行的。您是否考虑过将应用程序转换为服务?
public partial class MDIParent : ShellLib.ApplicationDesktopToolbar , IDisposable
{

    private static MDIParent MDIParentInstance = null;


    public MDIParent()
    {
        InitializeComponent();
        timer1.Interval = 50;
        timer1.Enabled = true;

    }
    int childCount = 1;
    int iHitcount = 0;


    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }


    private void MDIParent_FormClosed(object sender, FormClosedEventArgs e)
    {
        Taskbar.Show();
    }

    public void TabIt(string strProcessName)
    {
        //Get the collection of opened tab names and check against the new tabs.
        //If exists, dont allow to open the same tab again.

        bool found = false;

        if (Global.ExistingTabProcessNames.Count > 0)
        {
            foreach (string currentTab in Global.ExistingTabProcessNames)
            {
                if (currentTab.Equals(strProcessName))
                {
                    found = true;
                }
            }
        }

        if (found == false)
        {
            this.Show();

            //Creating MDI child form and initialize its fields
            MDIChild childForm = new MDIChild();
            childForm.Text = strProcessName;
            childForm.MdiParent = this;

            //child Form will now hold a reference value to the tab control
            childForm.TabCtrl = tabControl1;

            //Add a Tabpage and enables it
            TabPage tp = new TabPage();
            tp.Parent = tabControl1;
            tp.Text = childForm.Text;
            tp.Show();
            //child Form will now hold a reference value to a tabpage
            childForm.TabPag = tp;
            //Activate the MDI child form
            childForm.Show();
            childCount++;

            //Activate the newly created Tabpage
            tabControl1.SelectedTab = tp;
            tp.Height = tp.Parent.Height;
            tp.Width = tp.Parent.Width;
        }
    }

    private void MDIParent_Load(object sender, EventArgs e)
    {
        Edge = AppBarEdges.Top;
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll")]

    private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

    private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        iHitcount = iHitcount + 1;
        if (iHitcount != 1)
            BringFront(tabControl1.SelectedTab.Text.ToString());
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;

    private void BringFront(string ExecutablePath)
    {
        Process[] Processes = Process.GetProcesses();

        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Contains(ExecutablePath))
            {

                ShowWindowAsync(clsProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
            }
            else
            {
                ShowWindowAsync(clsProcess.MainWindowHandle, SW_SHOWMINIMIZED);
            }
        }
    }

    public static MDIParent MDIParentRef
    {
        get
        {
            if (MDIParentInstance == null)
                MDIParentInstance = new MDIParent();

            return MDIParentInstance;
        }
    }

    public void showScreen()
    {
        if (this.InvokeRequired == true)
        {
            this.Invoke(new MethodInvoker(this.showScreen));
        }
        else
        {

            this.Show();
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (Global.TabProcessNames.Count > 0)
        {
            foreach (string strProcessName in Global.TabProcessNames)
            {
                TabIt(strProcessName);
            }
        }
    } 
}