OK, here's what I've got now. It's just 2 simple programs... One simply
updates a label after each wait timeout and has a button which launches a
second app that fires the event in the first. It doesn't terminate the app
right now. All I have the event trigger is a change in the label. The event
fires, but my first app hangs when that happens.
Here's code:
"Main app"
Private message As String
Public Const WAIT_TIMEOUT As Int32 = &H102
Private active2 As Boolean = True
Public Sub blah(ByVal sender As Object, ByVal e As EventArgs) Handles
MyBase.Load
Dim thread As New System.Threading.Thread(AddressOf argh)
thread.Start()
While active2
Application.DoEvents()
End While
Me.Close()
End Sub
Private Sub argh()
Dim active As Boolean = True
Dim ptr As IntPtr = CreateEvent(IntPtr.Zero, True, False, "KillEvent")
While active
If WaitForSingleObject(ptr, 1000) <> WAIT_TIMEOUT Then
'active = False 'This is commented so that the app doesn't
actually die
message = "Yes!"
Else
message = (Int32.Parse(Label1.Text) + 1).ToString
End If
Label1.Invoke(New EventHandler(AddressOf labelupdate))
End While
active2 = False
CloseHandle(ptr)
End Sub
Private Sub labelupdate(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = message
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
OpenNETCF.Diagnostics.Process.Start("KillerApp.exe")
End Sub
"Killer app"
Private Sub kill(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ptr As IntPtr = CreateEvent(IntPtr.Zero, True, False, "KillEvent")
EventModify(ptr, EventType._SET)
CloseHandle(ptr)
Me.Close()
Application.Exit()
End Sub
P/Invoke Wrappers:
<DllImport("coredll.dll", EntryPoint:="WaitForSingleObject",
SetLastError:=True)> _
Public Shared Function WaitForSingleObject(ByVal hHandle As IntPtr,
ByVal dwMilliseconds As Int32) As Int32
End Function
<DllImport("coredll.dll", EntryPoint:="CreateEvent",
SetLastError:=True)> _
Public Shared Function CreateEvent(ByVal lpEventAttributes As IntPtr,
ByVal bManualReset As Boolean, ByVal bInitialState As Boolean, ByVal lpName
As String) As IntPtr
End Function
<DllImport("coredll.dll", EntryPoint:="CloseHandle",
SetLastError:=True)> _
Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
End Function
<DllImport("coredll.dll", EntryPoint:="EventModify",
SetLastError:=True)> _
Public Shared Function EventModify(ByVal hEvent As IntPtr, ByVal ef As
EventType) As Boolean
End Function
Thanks,
Rich