Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Singel Instance problem, (process.start and GetProcessesByName)

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
AWestEng
Asked:
AWestEng
  • 2
  • 2
  • 2
2 Solutions
 
apeterCommented:
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
 
apeterCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AWestEngAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
AWestEngAuthor Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now