Windows 当EC2实例关闭时,我如何才能优雅地关闭我的应用程序?

Windows 当EC2实例关闭时,我如何才能优雅地关闭我的应用程序?,windows,amazon-web-services,winforms,amazon-ec2,Windows,Amazon Web Services,Winforms,Amazon Ec2,我有一个数据处理应用程序,需要很多小时才能运行。我们的一位客户正在AWS EC2 Windows 2012实例上运行它,该实例定义了关机计划。因此,每次跑步时,它都会被切断一部分 这不会给数据处理本身带来问题:每个数据行都是原子处理的,之后可以安全地重新启动,而无需重新执行任何已处理的数据行。然而,在过程结束时,它确实会将大量摘要数据写入磁盘,我真的可以看到这些数据,但是这些数据并没有被写入 该应用程序是用VB.NET WinForms编写的。有一个表单在屏幕上显示进度,处理逻辑通过Backgr

我有一个数据处理应用程序,需要很多小时才能运行。我们的一位客户正在AWS EC2 Windows 2012实例上运行它,该实例定义了关机计划。因此,每次跑步时,它都会被切断一部分

这不会给数据处理本身带来问题:每个数据行都是原子处理的,之后可以安全地重新启动,而无需重新执行任何已处理的数据行。然而,在过程结束时,它确实会将大量摘要数据写入磁盘,我真的可以看到这些数据,但是这些数据并没有被写入

该应用程序是用VB.NET WinForms编写的。有一个表单在屏幕上显示进度,处理逻辑通过
BackgroundWorker
组件完成

该表单按如下方式处理取消事件:

Private Sub MyBase_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

    ' If the user tried to close the app, ask if they are sure, and if not, then don't close down.
    If e.CloseReason = CloseReason.UserClosing AndAlso ContainsFocus AndAlso Not PromptToClose() Then
        e.Cancel = True
        Return
    End If

    ' If the background worker is running, then politely request it to stop. Otherwise, let the form close down.
    If worker.IsBusy Then
        If Not worker.CancellationPending Then
            worker.CancelAsync()
        End If
        e.Cancel = True
    Else
        Environment.ExitCode = AppExitCode.Cancelled
    End If

End Sub
Private Sub ReportProgress(state as UIState)
    worker.ReportProgress(0, state)
    If worker.CancellationPending Then
        Throw New AbortException ' custom exception type
    End If
End Sub
后台线程对处理的每个数据行轮询一次取消,如下所示:

Private Sub MyBase_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

    ' If the user tried to close the app, ask if they are sure, and if not, then don't close down.
    If e.CloseReason = CloseReason.UserClosing AndAlso ContainsFocus AndAlso Not PromptToClose() Then
        e.Cancel = True
        Return
    End If

    ' If the background worker is running, then politely request it to stop. Otherwise, let the form close down.
    If worker.IsBusy Then
        If Not worker.CancellationPending Then
            worker.CancelAsync()
        End If
        e.Cancel = True
    Else
        Environment.ExitCode = AppExitCode.Cancelled
    End If

End Sub
Private Sub ReportProgress(state as UIState)
    worker.ReportProgress(0, state)
    If worker.CancellationPending Then
        Throw New AbortException ' custom exception type
    End If
End Sub
后台线程中的顶级
Catch
块在退出线程之前适当地处理所有成功和失败路径,将所有摘要数据写入磁盘

我已经在AWS之外进行了广泛的测试。特别是,我已经验证过,如果您通过Task Manager结束该过程,那么它会在不提示用户的情况下立即正常关闭,并按预期将所有摘要数据写入磁盘。正常关机最多需要几秒钟

我的问题是,当AWS按计划关闭EC2实例时,汇总数据没有写入磁盘。我不知道EC2关闭过程中到底发生了什么,我对EC2文档的搜索还没有阐明这一点。我想这可能是:

  • AWS或Windows正在终止正在运行的进程,而不发送
    WM\u CLOSE
    消息
  • AWS或Windows没有足够的时间让每个进程关闭

谁能澄清EC2关闭过程是如何工作的,或者建议我如何改进代码来处理它?

您是否尝试过处理此问题?@TnTinMn谢谢。我将尝试一下,看看是否可以为自己获取一个EC2实例来测试它。