Solved

Singel Instance problem, (process.start and GetProcessesByName)

Posted on 2008-10-07
6
335 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 250 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 85

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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