2

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..

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

Still Error

Is there any solution?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
YS HOME
  • 23
  • 3

1 Answers1

1

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
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29