Solved

Singel Instance problem, (process.start and GetProcessesByName)

Posted on 2008-10-07
6
330 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:AWestEng
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

by:AWestEng
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now