Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Process.Exited Button Event Exception

My Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Button1.Enabled = False
  Dim Process1 As New Process
  Process1.StartInfo.FileName = ("data\test.exe")
  Process1.EnableRaisingEvents = True
  AddHandler Process1.Exited, AddressOf OnExit
  Process1.Start()
End Sub
Sub OnExit(ByVal sender As Object, ByVal e As EventArgs)
  Button1.Enabled = True
End Sub

There is an error

I try..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sub OnExit(ByVal sender As Object, ByVal e As EventArgs)
  Procces1.Close()
  Button1.Enabled = True
End Sub

Still Error

Is there any solution?

>Solution :

Since OnExit is called by a different thread, you have to use the Control.BeginInvoke Method to modify Button1 properties.

You can use this Sub from this question (Crossthread operation not valid) to control the Enabled state:

Private Sub SetControlEnabled(ByVal ctl As Control, ByVal enabled As Boolean)
    If ctl.InvokeRequired Then
        ctl.BeginInvoke(New Action(Of Control, Boolean)(AddressOf SetControlEnabled), ctl, enabled)
    Else
        ctl.Enabled = enabled
    End If
End Sub

Updated code:

Sub OnExit(ByVal sender As Object, ByVal e As EventArgs)
    SetControlEnabled(Button1, True)
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading