[RESOLVED] [02/03] Single Instance application on Vista
Hello everyone.
I'm used to doing this in VB.NET 2003:
Code:
If (UBound(GetProcessesByName(GetCurrentProcess.ProcessName)) > 0) Then
MessageBox.Show("Already Running!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
Now, the purpose of the above code is to ensure that my application cannot be run twice, ie. a single instance application.
The above code works nicely on XP, but on Vista it produces the following exception:
Quote:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll
Additional information: Couldn't get process information from remote machine.
Any advice on how I can achieve the same effect on Vista too?
Re: [02/03] Single Instance application on Vista
Not tested if gives right result yet but using this full string gives no error on mine.
If UBound(Diagnostics.Process.GetProcessesByName _
(Diagnostics.Process.GetCurrentProcess.ProcessName)) _
> 0 Then
Re: [02/03] Single Instance application on Vista
The application framework provides a single instance option. If you'd rather not use that, then you should use a mutex to indicate that your application is already open; this is preferable to scanning for processes by name. I don't know why the error occurs with your code but Vista is often more picky about such things.
Also, as a point of style, you should use the Length property of an array rather than the UBound function.
Re: [02/03] Single Instance application on Vista
Ok, Undertand what you mean, but what is a mutex?
Can I have an example of its usage?
Re: [02/03] Single Instance application on Vista
Example:
VB.NET: Single Instance Application (post 2)
I shouldn't have mentioned the application framework in my post earlier; I didn't notice that you said you were using 2003. My apologies.
Re: [02/03] Single Instance application on Vista
No probs, I know VB 2005 makes this much much easier :)
Thanx for that wonderful thread! :thumb:
I did attempt the Mutex route, but it didn't seem to work :(
I prefer going the Mutex route now, because I trust you and your advice (which is always solid!)
This is what I did:
Code:
Private bMutex As Mutex
Public Sub main()
bMutex = New Mutex(False, "aUniqueName")
If (Not bMutex.WaitOne(0, False)) Then
MessageBox.Show("Application is Running")
Else
MessageBox.Show("Application Not Running")
bMutex.ReleaseMutex()
Application.Run(New frmPersonalDet)
End If
End Sub
It ran the program twice, and both times it said that it is not running. What should I enter under "aUniqueName", is it my form's title, or program's title?
Sorry for these questions, I just want to make it work, and once I got it working, I can study the code.
Re: [02/03] Single Instance application on Vista
Bugger.
Here's another sample; see if that one works!
The "singleton design" link in that thread also looks quite interesting, but you probably don't want to make any architectural changes to your application at this stage.
Re: [02/03] Single Instance application on Vista
Ah, nevermind (thanx anyways!)
I did this:
Code:
Public Sub main()
Dim mutex As System.Threading.Mutex = New System.Threading.Mutex(False, "Thisshouldonlyrunonce")
Dim Running As Boolean = Not mutex.WaitOne(0, False)
If Running Then
MessageBox.Show("Already Running!")
Application.Exit()
Else
Application.Run(New frmAppPersInfo)
End If
End Sub
And it works like a charm! Now I just need to study the code :)
Re: [02/03] Single Instance application on Vista
Didn't see your reply sir :blush:
I see now, that the whole trick is to have a boolean variable, and based on it being true / false, we either show the form, or exit the program. Funky stuff!