|
-
Aug 15th, 2007, 02:00 AM
#1
Thread Starter
Hyperactive Member
[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:
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?
Last edited by GrimmReaper; Aug 15th, 2007 at 05:43 AM.
-
Aug 15th, 2007, 05:35 AM
#2
Addicted Member
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
-
Aug 15th, 2007, 05:52 AM
#3
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.
-
Aug 15th, 2007, 09:18 AM
#4
Thread Starter
Hyperactive Member
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?
-
Aug 15th, 2007, 09:23 AM
#5
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.
Last edited by penagate; Aug 15th, 2007 at 09:29 AM.
-
Aug 15th, 2007, 11:44 AM
#6
Thread Starter
Hyperactive Member
Re: [02/03] Single Instance application on Vista
No probs, I know VB 2005 makes this much much easier 
Thanx for that wonderful thread! 
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.
-
Aug 15th, 2007, 11:50 AM
#7
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.
-
Aug 15th, 2007, 11:50 AM
#8
Thread Starter
Hyperactive Member
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
-
Aug 15th, 2007, 11:52 AM
#9
Thread Starter
Hyperactive Member
Re: [02/03] Single Instance application on Vista
Didn't see your reply sir 
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|