I have some code for starting a process (notepad), and dynamically makes a event handler for the Exited event.
Private WithEvents notepad As New Process
notepad.StartInfo.FileName = "notepad.exe"
notepad.StartInfo.Arguments = fil
notepad.EnableRaisingEvents = True
notepad.Start()
AddHandler notepad.Exited, AddressOf ExitNotepad
But when I try to change the text of a label in the Event handler the application just closes, without any error messages at all.
Sub ExitNotepad(ByVal sender As System.Object, ByVal e As System.EventArgs)
lblLabel.Text = "Test"
End Sub
Anyone got any ideas?
You need to make it thread-safe, as the event handler may happen in a different thread. Also, I added the handler before starting the process. I used a Button to initiate starting notepad, and my label is named Label1:
And I gave it an explicit path to notepad.exe – always use explicit paths where possible.
Tested as working on Windows 7 x64 using the Visual Studio 11 Beta.