|
-
Feb 25th, 2004, 02:46 PM
#1
Thread Starter
Junior Member
is there an easy way to tell if my program is already running
Is there any way to check if another instance of my program is running if a user executes it ?
I would like to warn the user if the program is already running to prevent multiple instances.
I am new to vb.net so any help is greatly appreciated.
Thanks
--
Tony
-
Feb 25th, 2004, 03:25 PM
#2
Sleep mode
VB Code:
'Create a new instance of your main form
Dim MainForm As New Form1()
Private Sub MultiCheck()
'Get number of processes of you program
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
'Ask user if they want to run another copy
If MessageBox.Show("There is already a copy of this program." & vbCrLf _
& "Do you want to launch another?.", _
"Launch another copy of this program?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button1) = DialogResult.No Then
'If no, then exit the new instance of the program
Application.Exit()
Exit Sub
Else
'Else run the new program
MainForm.ShowDialog()
Exit Sub
End If
End If
'If this is the only one, run it!
MainForm.ShowDialog()
End Sub
-
Feb 25th, 2004, 03:35 PM
#3
Sleep mode
Btw , there's another way by using :Mutex . Both lead to the same result though .
-
Feb 25th, 2004, 05:39 PM
#4
Thread Starter
Junior Member
This looks just the ticket. What is mutex ?
I have never heard of that. All my prior programming had been in VB6. Is it something new to .net ?
Thanks
Tony
-
Feb 25th, 2004, 08:17 PM
#5
didnt they add a property or something in vs2003 that was like the old prevInstance in vb6? or am I thinking of something else?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 26th, 2004, 01:59 AM
#6
Sleep mode
Originally posted by TonyVR4
This looks just the ticket. What is mutex ?
I have never heard of that. All my prior programming had been in VB6. Is it something new to .net ?
Thanks
Tony
Yes , it's new to .NET . Ex :
http://www.freevbcode.com/ShowCode.asp?ID=5845
-
Feb 26th, 2004, 02:00 AM
#7
Sleep mode
Originally posted by MrPolite
didnt they add a property or something in vs2003 that was like the old prevInstance in vb6? or am I thinking of something else?
I don't think so . They added some 'easy' and indirect ways to this .
-
Feb 26th, 2004, 09:43 AM
#8
yay gay
It's not something new at all, it existed since the very beggining in the win API
\m/  \m/
-
Feb 26th, 2004, 09:46 AM
#9
Sleep mode
Originally posted by PT Exorcist
It's not something new at all, it existed since the very beggining in the win API
No , it's new to .NET . I mean as managed code . Ofcourse it was based on API .
-
Feb 26th, 2004, 02:40 PM
#10
Thread Starter
Junior Member
I tried this and it sits there for a long time, then I get and error on the line where I do the ShowDialog "Error Creating Window Handle"
Here is the code I put into my form load. My form is called frmMain
Dim MainForm As New frmMain
'Get number of processes of you program
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
MessageBox.Show("There is already a copy of this program running", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'If another instance is running then exit
Application.Exit()
Exit Sub
Else
'Else run the new program
MainForm.ShowDialog()
Exit Sub
End If
-
Feb 26th, 2004, 04:36 PM
#11
Sleep mode
Where did you add this code ?
-
Feb 27th, 2004, 04:31 PM
#12
Thread Starter
Junior Member
I added it in the beginning of the form load event for my main form
Thanks
Tony
-
Mar 1st, 2004, 02:15 PM
#13
New Member
I am trying to get the same information.
In a button's click event on my form I call my PrevInstance function. In that function I have both Pirate's method and the method I found in vb.net help.
Pirates always returns 0. the Help's method always returns -1.
Public Function PrevInstance() As Boolean
MsgBox(Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length)
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
Can anyone see the problem?
Thanks,
Robb
-
Mar 1st, 2004, 02:51 PM
#14
I would suggest the using a mutex instead. Its about ten times faster than querying the processes.
http://www.vbforums.com/showthread.p...hreadid=260721
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
|