Results 1 to 14 of 14

Thread: is there an easy way to tell if my program is already running

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. 'Create a new instance of your main form
    2.     Dim MainForm As New Form1()
    3.  
    4.     Private Sub MultiCheck()
    5.         'Get number of processes of you program
    6.         If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
    7.             'Ask user if they want to run another copy
    8.         If MessageBox.Show("There is already a copy of this program." & vbCrLf _
    9.             & "Do you want to launch another?.", _
    10.             "Launch another copy of this program?", _
    11.             MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
    12.             MessageBoxDefaultButton.Button1) = DialogResult.No Then
    13.             'If no, then exit the new instance of the program
    14.                 Application.Exit()
    15.                 Exit Sub
    16.             Else
    17.             'Else run the new program
    18.                 MainForm.ShowDialog()
    19.                 Exit Sub
    20.             End If
    21.         End If
    22.         'If this is the only one, run it!
    23.         MainForm.ShowDialog()
    24.     End Sub

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Btw , there's another way by using :Mutex . Both lead to the same result though .

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    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

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Pirate
    Yes , it's new to .NET . Ex :

    http://www.freevbcode.com/ShowCode.asp?ID=5845
    It's not something new at all, it existed since the very beggining in the win API
    \m/\m/

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    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

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Where did you add this code ?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    I added it in the beginning of the form load event for my main form

    Thanks
    Tony

  13. #13
    New Member
    Join Date
    Feb 2004
    Posts
    8
    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

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width