私は、VB.Net を使用してスケジューラのようなプロジェクトに取り組んでいます。このアプリケーションは、Application.Run() を使用して「Sub Main」から開始します。すべてのプログラム コードは、クラス内のハンドラであり、ここで作成および開始されます。
Public Sub Main()
m_App = New myApp
m_App.Start()
Application.Run()
End Sub
myApp 内には、タスクの実行を制御するタイマーがあり、各タスクのスレッドを開始します。タスクが完了すると、エラーが検出された場合に警告ウィンドウを表示しようとします。警告ウィンドウ (frmAlert) を表示するために、実行スレッドとメイン スレッドの間で通信するための 2 つの異なる方法をテストしました。
1) タスク オブジェクトに pulic イベントを追加してから、メイン スレッドの関数にハンドラを追加する
2) デリゲートを使用してメイン スレッドに通知する
ただし、警告ウィンドウは表示できず、エラーは報告されていません。IDE でデバッグした後、アラート ウィンドウが正常に表示されたが、タスク スレッドが完了すると閉じてしまうことがわかりました。
これは単純化されたタスククラスです (2 つのコミュニゼーション方法によるテスト)。
Public Class myProcess
Public Event NotifyEvent()
Public Delegate Sub NotifyDelegate()
Private m_NotifyDelegate As NotifyDelegate
Public Sub SetNotify(ByVal NotifyDelegate As NotifyDelegate)
m_NotifyDelegate = NotifyDelegate
End Sub
Public Sub Execute()
System.Threading.Thread.Sleep(2000)
RaiseEvent NotifyEvent()
If m_NotifyDelegate IsNot Nothing Then m_NotifyDelegate()
End Sub
End Class
そしてメインのアプリケーションクラス
Imports System.Threading
Public Class myApp
Private WithEvents _Timer As New Windows.Forms.Timer
Private m_Process As New myProcess
Public Sub Start()
AddHandler m_Process.NotifyEvent, AddressOf Me.NotifyEvent
m_Process.SetNotify(AddressOf NotifyDelegate)
ProcessTasks()
End Sub
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick
ProcessTasks()
End Sub
Public Sub ProcessTasks()
_Timer.Enabled = False
'
Dim m_Thread = New Thread(AddressOf m_Process.Execute)
m_Thread.Start()
'
_Timer.Interval = 30000
_Timer.Enabled = True
End Sub
Public Sub NotifyEvent()
frmAlert.Show()
End Sub
Public Sub NotifyDelegate()
frmAlert.Show()
End Sub
End Class
NotifyEvent または NotifyDelegate のいずれかを使用して frmAlert が表示されますが、Execute が完了するとすぐに閉じられることがわかりました。
ユーザーが閉じるまで画面に留まることができる実行スレッドから警告ウィンドウをポップアップする方法を教えてください。
前もって感謝します!