如何在VB.NET中关闭表单之前完成任务 [英] How to complete task before form closing in VB.NET

查看:61
本文介绍了如何在VB.NET中关闭表单之前完成任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算开发一个电子邮件计划应用程序。



我想在申请表关闭时发送报告邮件



我打电话给我的子表格关闭子..但它不起作用。



请帮助我。< br $> b $ b

我尝试过:



我的vb.net代码如下



I am going to developing an application for email Scheduling.

I want to sent an report mail when application get closed at form closing event

I call a my sub in form closing sub.. but it doesn't working.

please help me.

What I have tried:

my vb.net code as follow

Private Sub CloseForm(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            Dim MBox As New MyMessageBox
            MBox.ShowDialog()

            If MBox.GetDecision Then
                tmrSendMessage.Stop()
                SendReportMail(Now.Date, Now.Date.AddDays(1)) 'This is a sub that I want to call at form closing
            Else
                e.Cancel = True
            End If


        Catch ex As Exception
            MessageBox.Show(Err.Description, "Form Closing", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub



'***************************************************
 Private Sub SendReportMail(ByVal TodaysDate As Date, ByVal NextDate As Date)
        Try
            lblStatus.Text = "Sending Report Mail"
            Application.DoEvents()
            Dim cmdGeteport As SqlCommand
            Dim CounterReader As SqlDataReader
            cmdGeteport = New SqlCommand("GetReport", MySqlconnection)
            cmdGeteport.CommandType = CommandType.StoredProcedure

            cmdGeteport.Parameters.AddWithValue("@Date1", TodaysDate)
            cmdGeteport.Parameters.AddWithValue("@Date2", NextDate)
            CounterReader = cmdGeteport.ExecuteReader()

            If CounterReader.Read Then

                Dim isSent As Boolean
                Dim ReportText As String
                Dim TemplatStreamReader As New IO.StreamReader(Application.StartupPath & "\ReportSend.txt")

                ReportText = TemplatStreamReader.ReadToEnd
                ReportText = ReportText.Replace("@MSchedule", CInt(CounterReader("ScheduleMail")))
                ReportText = ReportText.Replace("@SSchedule", CInt(CounterReader("ScheduleSMS")))
                ReportText = ReportText.Replace("@MSent", CInt(CounterReader("SentMail")))
                ReportText = ReportText.Replace("@SSent", CInt(CounterReader("SentSMS")))
                ReportText = ReportText.Replace("@MExpired", CInt(CounterReader("ExpiredMail")))
                ReportText = ReportText.Replace("@SExpired", CInt(CounterReader("ExpiredSMS")))
                ReportText = ReportText.Replace("@MFailed", CInt(CounterReader("FailedMail")))
                ReportText = ReportText.Replace("@SFailed", CInt(CounterReader("FailedSMS")))
                ReportText = ReportText.Replace("@ActiveApplications", CInt(CounterReader("ActiveApplications")))
                ReportText = ReportText.Replace("@BlockedApplications", CInt(CounterReader("BlockedApplications")))
                ReportText = ReportText.Replace("@ReportDate", TodaysDate)
                SendEmailMessage("Hermes", "vikashire3@gmail.com ", "readcode ", "Vikashire3@gmail.com", "smtp.gmail.com", "25", "Vikashire@yahoo.com", "Hermes Report", ReportText, "", isSent, Me.ToString)
            End If
            CounterReader.Close()
            lblStatus.Text = ""
            Application.DoEvents()

        Catch
            MessageBox.Show(Err.Description, "Send Report", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub







显示在调试期间调用..但我没有收到任何邮件..




It shows calling during debugging.. but i didn't got any mail..

推荐答案

主要的问题是你正在调用Application.DoEvents - 这绝不是一个好主意,在这种情况下会导致表单在代码仍然存在时完成关闭运行。

您可以尝试从SendReportMail方法中删除调用,它应该意味着它将工作 - 但是...测试很多。我有理由相信SendMailMessage不能异步工作 - 因为它有一个异步版本 - 但很可能实际的邮件是由一个单独的进程执行的,关闭表单/应用程序也可能会终止该进程。 br />
我很想设置一个无UI的第二个应用程序来处理报告邮件,并在我的应用程序关闭时将其关闭以分离这两个函数。
The main problem is that you are calling Application.DoEvents - which is never a good idea, and in this case causes the form to finish closing while your code is still running.
You could try removing the call from your SendReportMail method, and it should mean it will work - but ... test it lots. I'm reasonably sure that SendMailMessage doesn't work asynchronously - since it has an async version - but it's quite possible that the actual mailing is performed by a separate process, and that closing the form / application may terminate that process as well.
I'd be tempted to set up a UI-less second app to handle the report mailing, and kick that off when my app closed just to separate the two functions.


这篇关于如何在VB.NET中关闭表单之前完成任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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