检测主 UI 是否正在关闭 [英] Detect if Main UI is closing

查看:32
本文介绍了检测主 UI 是否正在关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个类库.我找不到检测主 UI 是否关闭的方法.

I'm currently developing a class library. I'm having trouble finding a way to detect if the Main UI is closing.

有没有办法从类库项目中知道这一点?

Is there a way to know that from the class library project?

我这样做的目的是在主 UI 关闭时取消当前正在运行的所有线程.

My purpose of this is to cancel all the threads that is currently running if the Main UI is closing.

推荐答案

首先,在您的 DLL 中,引用 Syste.Windows.Forms 类库然后,在您的 DLL 构造函数中,为 Application Exit 添加事件,如下所示:

First, in your DLL, reference the Syste.Windows.Forms class library Then, in your DLL constructor, add the event for Application Exit as follows:

public class MyDLLClass
{
    public MyDLLClass()
    {
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    }

    void Application_ApplicationExit(object sender, EventArgs e)
    {
        //Your thread handling code...
    }
}

就是这样!

在我的 Windows 窗体中测试:

  public Form1()
    {
        InitializeComponent();

        MyDLL_NameSpace.MyDLLClass ff = new MyDLL_NameSpace.MyDLLClass();
    }

调试(关闭应用程序):

这篇关于检测主 UI 是否正在关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆