Question How to make a .exe form open only once?

phoebe_ccy

Member
Joined
Oct 12, 2009
Messages
6
Programming Experience
Beginner
I have a vb.net form which has a button in it. The button will call A.exe (it's a vb.net exe) using the command Shell("A.exe ", AppWinStyle.NormalFocus).

How can I make sure that everytime I press on the button, A.exe will only open once, and does not open a new form each time?

If A.exe is already opened, it will bring to front. If it is not open, then A.exe will be opened. Please help.
 
First up, don't use Shell in VB.NET. Use Process.Start to start a process. You can then test the HasExited property of that Process object later to see whether it has exited. If it has then you can start a new instance. If it hasn't then, assuming that it has a main window, you can call AppActivate to activate it. Just note that AppActivate won't restore the window if it's minimised. You'd have to mess around with the Windows API for that.
 
Another way is to check for existing Process instances:
VB.NET:
Dim pc() = Process.GetProcessesByName("calc")
If pc.Length > 0 Then
    AppActivate(pc(0).Id)
Else
    Process.Start("calc.exe")
End If
 
Back
Top