我有一个应用程序,我需要在它处于非活动状态或未使用后关闭。我写了下面的代码,我正在
windowsBase.dll中的System.InvalidOperationException
你知道怎么做到这一点吗?我需要应用程序不要使用某些线程计时器“冻结”。
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 9000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Close()
End Sub
答案 0 :(得分:1)
尝试:
Dim app as Application
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
app.Exit()
End Sub
这似乎对我有用。
以下是我在测试您的问题时使用的空白表单上的完整代码。我得到了你尝试使用Me.Close()但我的Application.Exit()和app.Exit()工作没有问题的smae异常。
Public Class Form1
Dim app As Application
Private Sub MainWindow_Loaded(sender As Object, e As EventArgs) Handles Me.Load
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 5000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
'Environment.Exit(0) - closes without a problem
'Application.Exit() - closes without a problem
'closes without a problem.
app.Exit()
'Me.Close() - System.InvalidOperationException
End Sub
结束班