有没有办法在 vb.net windows 窗体系统中捕获应用程序退出事件? [英] Is there any way to capture application exit event in vb.net windows form system?

查看:27
本文介绍了有没有办法在 vb.net windows 窗体系统中捕获应用程序退出事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我曾经在 program.cs 中使用以下代码捕获应用程序退出事件.

In C#, I used to capture the application exit event with the following code in program.cs.

   static void OnProcessExit(object sender, EventArgs e)
    {
        string message = "The application was closed at :" + DateTime.Now.ToString() + "."; 


    }

在 VB.NET 中与此等效的是什么?

What is the equivalent of this in VB.NET?

如果数据库连接在应用程序退出事件中打开,我想关闭它.

I want to close the database connection if it is open by any chance in the application exit event.

推荐答案

试试这个

AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' When the application is exiting, write the application data to the 
    ' user file and close it.
    WriteFormDataToFile()

    Try 
        ' Ignore any errors that might occur while closing the file handle.
        userData.Close()
    Catch 
    End Try 
End Sub

来源

这篇关于有没有办法在 vb.net windows 窗体系统中捕获应用程序退出事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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