开发者

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

开发者 https://www.devze.com 2023-02-10 13:43 出处:网络
In my form, when the user clicks a button I launch another executable. That executable displays another window on the screen.

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

Ho开发者_如何学Gowever, 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消