herbert
2009-12-18 12:42:01 UTC
The following app raises an event within its own event handler.
This RaiseEvent causes the first execution of the event handler being
interrupted, and a second execution runs.
The two event handlers runs in the same thread as the code where RaiseEvent
is fired. Using the typical lock mechanisms protecting data therefore fail,
as they lock between different threads only.
How can I prevent multiple executions of the same event handler in the same
thread? I.e. how can I queue Events?
Do I have to redesign the software and use my own request FIFO queue instead
of Events? Should I use StateMachine WorkFlow?
Thank you very much. herbert
Imports System.Threading
Module Module2
Dim WithEvents mPerson As New Person
Dim mSingleShotDone As Boolean = False 'to prevent StackOverflow
Sub Main()
Console.WriteLine("Main Thread ID: " &
Thread.CurrentThread.ManagedThreadId)
Call mPerson.OnPersonEvent()
Console.ReadLine()
End Sub
'event handler are always executed in the thread were the event is raised
Private Sub Person_PersonEvent() Handles mPerson.PersonEvent
Counter.Increment()
Dim localCount As Integer = Counter.Count
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
Thread ID: " & Thread.CurrentThread.ManagedThreadId & "; Count: " &
localCount)
If Not mSingleShotDone Then
mSingleShotDone = True
Call mPerson.OnPersonEvent() 'this RaiseEvent interrupts the
current execution of Person_PersonEvent() and starts another execution of it.
End If
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
sleeping." & " Count: " & localCount)
Thread.Sleep(3000)
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
exit." & " Count: " & localCount)
End Sub
End Module
This RaiseEvent causes the first execution of the event handler being
interrupted, and a second execution runs.
The two event handlers runs in the same thread as the code where RaiseEvent
is fired. Using the typical lock mechanisms protecting data therefore fail,
as they lock between different threads only.
How can I prevent multiple executions of the same event handler in the same
thread? I.e. how can I queue Events?
Do I have to redesign the software and use my own request FIFO queue instead
of Events? Should I use StateMachine WorkFlow?
Thank you very much. herbert
Imports System.Threading
Module Module2
Dim WithEvents mPerson As New Person
Dim mSingleShotDone As Boolean = False 'to prevent StackOverflow
Sub Main()
Console.WriteLine("Main Thread ID: " &
Thread.CurrentThread.ManagedThreadId)
Call mPerson.OnPersonEvent()
Console.ReadLine()
End Sub
'event handler are always executed in the thread were the event is raised
Private Sub Person_PersonEvent() Handles mPerson.PersonEvent
Counter.Increment()
Dim localCount As Integer = Counter.Count
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
Thread ID: " & Thread.CurrentThread.ManagedThreadId & "; Count: " &
localCount)
If Not mSingleShotDone Then
mSingleShotDone = True
Call mPerson.OnPersonEvent() 'this RaiseEvent interrupts the
current execution of Person_PersonEvent() and starts another execution of it.
End If
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
sleeping." & " Count: " & localCount)
Thread.Sleep(3000)
Console.WriteLine(Date.Now.ToLongTimeString & " PersonEvent Handler
exit." & " Count: " & localCount)
End Sub
End Module