Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Singel Instance problem, (process.start and GetProcessesByName)

Posted on 2008-10-07
6
Medium Priority
?
344 Views
Last Modified: 2013-11-07
Hi!

I got a problem, I use this for making the application a singel instance app

<STAThread()> _
Public Shared Sub Main(ByVal args As String())
      Dim RunningProcesses As Process() = Process.GetProcessesByName("TestApp")
      If (RunningProcesses.Length = 0) Then
            Application.Run(New frmMain)
       end if
End Sub

But if I Do this from another application

Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = applicationPath & "TestApp.exe"
 myProcess.Start()

I then get a problem, when the GetProcessesByName methods is executed the process will  already be up and running and the applcaition will not start as it should.

so any tips how to fix this?
0
Comment
Question by:AWestEng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 1000 total points
ID: 22659607
Use Mutex to have only one instance of an appliction to run at a time. Hope below helps


Public Shared Sub Main(ByVal args As String())

Private m_UniqueIdentifier As String  'ASSIGN A UNIQUE IDENTIFIER
Private m_Mutex As System.Threading.Mutex

' Create the Mutex class
    m_Mutex = New System.Threading.Mutex(False, m_UniqueIdentifier)
    ' Attempt to lock the Mutex
   
    ' We locked it! We are the first instance!!!    
    If m_Mutex.WaitOne(1, True) Then
        CreateInstanceChannel()
        ' Raise the StartUpEvent as a NewInstance
        Dim event_args As New StartUpEventArgs(True, args)
        RaiseStartUpEvent(event_args)
    Else
        ' Not the first instance!!!
        ' Raise the StartUpEvent through the
        ' SingletonCommunicator not as a NewInstance
        Dim event_args As New StartUpEventArgs(False, args)
        UseInstanceChannel(event_args)
    End If
End Sub
0
 
LVL 25

Expert Comment

by:apeter
ID: 22659627
Please consider only IF ELSE statment to check for first or second instance. Inside If and Else Statments codes are the actions u can take accordingly.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 22660658
You have this for your Tags:

    Tags: VS 2005: VB.Net

If you are really using VB.Net 2005, then this is already BUILT-IN for you...

Go to Project --> Properties and make sure you are on the Application Tab.
Check the "Enable Application Framework" box.
Set the "Startup Form" to frmMain.
Down Below, check the "Make Single Instance Application" box.

DONE!

If you want to trap the event that fires when multiple instances are launched, see the Application.StartupNextInstance() event:
http://msdn.microsoft.com/en-us/library/b9z4eyh8(VS.80).aspx

To get this event, click on the "View Application Events" button in the bottom right of the Application Tab in the Project Properties.
Namespace My
 
    ' The following events are availble for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
 
        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            e.BringToForeground = True
        End Sub
 
    End Class
 
End Namespace

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 1

Author Comment

by:AWestEng
ID: 22662466
I can't use the built-in stuff, I do alot more in the Sub Main() then I show in the code above.
I also use the context to add som hotkey stuff into the application.
sorry for the poorly information I provided..
 
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22662526
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22662822
yepp  :) that is correct,
I used the  
Dim RunningProcesses As Process() = Process.GetProcessesByName("TestApp")
and all worked just fine untill I started the application from another app.

0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question