Results 1 to 10 of 10

Thread: Disallow Multiple Instances of an app and focus to first instance?

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Disallow Multiple Instances of an app and focus to first instance?

    I know how to only allow 1 instance (using a mutex). But I don't know how to bring focus to the original application (main form) when the user tries to open a new instance. Anyone know how?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't have any code handy at the moment but I believe its AppActivate or some such.

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    A while back i tried to find a way to get the Mutex owner process name or ID but i couldnt figure out how.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thanks Edneeis,
    It has recommended this:
    'Only works if the title of the application is the same as the ProductName.
    AppActivate(Application.ProductName)
    But still not a guranteed way. What is needed is the Proccess ID of the Mutex Owner.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes or the title of the application to activate, but I haven't had to deal with that yet. I so far have always known the title of the application.

  7. #7

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    My title is also always constant, so the link is exactly what I was looking for. I put the AppActivate("xxxxx") around a Try Catch just to be sure.

    But I would much rather use the ProcessID as suggested. Now, on to figuring our how to get the ProcessID...

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Hey man, I didn't notice you have recommended that in that forum.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9
    Addicted Member
    Join Date
    Oct 2001
    Location
    Between 2 bosses
    Posts
    210

    Re: Disallow Multiple Instances of an app and focus to first instance?

    It has been a long time since you asked for this. I was just browsing and thought I would pass this along. You wanted to activate by Process ID. Since Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length returns >1, if the application is already open, you must compare the current process id to the ones found to figure out which one to activate. Hope this helps anyone.




    VB Code:
    1. Dim current As Process = Process.GetCurrentProcess()
    2.  
    3.         Try
    4.             If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
    5.                 Try
    6.                     For i As Integer = 0 To (Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length - 1)
    7.                         If (Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)(i).Id) <> current.Id Then
    8.                             AppActivate(Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)(i).Id)
    9.                         End If
    10.                     Next
    11.                 Catch ex As Exception
    12.                     ' Add code as desired
    13.                 End Try
    14.                 Return True
    15.             Else
    16.                 Return False
    17.             End If
    18.         Catch ex As Exception
    19.             Return False
    20.         End Try

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Re: Disallow Multiple Instances of an app and focus to first instance?

    Thanks RSJIG for the suggestion. But I'm afraid the main problem which was getting the process name or ID of a mutex owner still remains. This post is about 4 years old and back to .NET FX 1.0 time. So there might be ways in newer frameworks.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

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