mohit nagpal
mohit nagpal

Reputation: 151

Executable should not be started again if already started on button click in windows form

In my form, when the user clicks a button I launch another executable. That executable displays another window on the screen.

However, when the user clicks on the button a second time, I don't want to display a second window if the first one is still open.

How can I do this?

Upvotes: 2

Views: 157

Answers (2)

Emond
Emond

Reputation: 50712

You can disable the button, then use Process.Start and simply wait for the Exited event of the returned Process object to occur before enabling the button again.

EDIT Simply disabling the button is not a correct way of preventing the process from being started again; it is just a sign to the user that the button can't be clicked again. You should use a flag/ManualResetEvent and set it when the process starts. Check for it before starting the process and reset it in the Exited event.

Upvotes: 2

John Koerner
John Koerner

Reputation: 38087

Are you using Process.Start to launch the executable? If so, you could keep the process ID around and see if the process is still running. If it is, do nothing, if not, relaunch it.

Upvotes: 0

Related Questions