J
John Lafrowda
Dear all,
I'm currently having some trouble with the event mechanisms in the .net
framework. What I am looking for is a way to schedule events like it is done
with PostMessage in the WinAPI: When I raise an event, the raising code
should be continued. The scheduled event should just be called after
execution of the current function has terminated. In WinAPI programming,
this could be done by posting a message through PostMessage which would be
invoked not earlier as the calling thread returns to the central message
loop.
The .net framework, however, handles events more in a "SendMessage" style,
meaning that the event handler is called immediately and interrupts the
calling code.
Cosider the following simple example in VB.net (the mechanism would be
slightly different in C# as the RaiseEvent statement is missing here - the
effect is nevertheless the same with C# techniques):
Provide a form class (Form1) with a single button (Button1) on it:
==================================================================
Public Class Form1
Inherits System.Windows.Forms.Form
[#Region " Windows Form Designer generated code "]
Private WithEvents MyEventObj As New MyEventClass
Private Sub MyHandler() Handles MyEventObj.MyEvent
MsgBox("During Event")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
Button1.Click
MsgBox("Before Call")
MyEventObj.RaiseIt()
MsgBox("After Call")
End Sub
End Class
==================================================================
In addition, the MyEventClass is needed with following contents:
==================================================================
Public Class MyEventClass
Public Event MyEvent()
Public Sub RaiseIt()
RaiseEvent MyEvent()
End Sub
End Class
==================================================================
Now, the message boxes are appearing in the sequence "Before Call" ->
"During Event" -> "After Call", where I would like to see the sequence
"Before Call" -> "After Call" -> "During Event".
Probably the .net event model is the wrong way to go, but I don't see any
other at the moment except for going back to the WinAPI with PostMessage,
which does not provide the features of managed code (and object/contents
transmission) and is thus not exactly what I want, either.
Maybe I don't see the obvious here... Any solutions?
Best regards,
John
I'm currently having some trouble with the event mechanisms in the .net
framework. What I am looking for is a way to schedule events like it is done
with PostMessage in the WinAPI: When I raise an event, the raising code
should be continued. The scheduled event should just be called after
execution of the current function has terminated. In WinAPI programming,
this could be done by posting a message through PostMessage which would be
invoked not earlier as the calling thread returns to the central message
loop.
The .net framework, however, handles events more in a "SendMessage" style,
meaning that the event handler is called immediately and interrupts the
calling code.
Cosider the following simple example in VB.net (the mechanism would be
slightly different in C# as the RaiseEvent statement is missing here - the
effect is nevertheless the same with C# techniques):
Provide a form class (Form1) with a single button (Button1) on it:
==================================================================
Public Class Form1
Inherits System.Windows.Forms.Form
[#Region " Windows Form Designer generated code "]
Private WithEvents MyEventObj As New MyEventClass
Private Sub MyHandler() Handles MyEventObj.MyEvent
MsgBox("During Event")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
Button1.Click
MsgBox("Before Call")
MyEventObj.RaiseIt()
MsgBox("After Call")
End Sub
End Class
==================================================================
In addition, the MyEventClass is needed with following contents:
==================================================================
Public Class MyEventClass
Public Event MyEvent()
Public Sub RaiseIt()
RaiseEvent MyEvent()
End Sub
End Class
==================================================================
Now, the message boxes are appearing in the sequence "Before Call" ->
"During Event" -> "After Call", where I would like to see the sequence
"Before Call" -> "After Call" -> "During Event".
Probably the .net event model is the wrong way to go, but I don't see any
other at the moment except for going back to the WinAPI with PostMessage,
which does not provide the features of managed code (and object/contents
transmission) and is thus not exactly what I want, either.
Maybe I don't see the obvious here... Any solutions?
Best regards,
John