Solved

Singel Instance problem, (process.start and GetProcessesByName)

Posted on 2008-10-07
6
340 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 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 86

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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