Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted
I found this in the MSDN.
' Visual Basic .NET
Function PrevInstance() As Boolean
  If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
     Return True
  Else
     Return False
  End If
End Function

Posted

Great! Thanks .. I did search the Microsoft Site, but I didn't come up with anything (just the VB6 one I posted above) .. Is there a way to have it set the focus to the already existing instance?

 

M.

  • *Experts*
Posted

You can get the handle of the application's main window by using

the .MainWindowHandle property of the process you found.

I don't know, but I assume the first one started will be 0 in the

array of returned processes.

Posted

The following code will recognize when a multiple instance of an application has been run:

 

' Prevent Multiple Instances of the Application
Dim appProcesses As Process() = _
  Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

If appProcesses.Length > 1 Then
  ' Terminate This Instance
End If

This code is placed on the main form and after MyBase.New() and before the InitializeComponent() ...

 

However, this leads to my next question ... I can't terminate the instance with a simple Me.Close() :confused: It just continues to execute, the code works because I've put a message box with the info in it and it works ... Is there another way I can terminate this instance?

 

Thanks!

M.

Posted

That's a new command I didn't know Derek, but still doesn't work .. here is the code that I have on my form ... I tried moving the "IF ... THEN" after the SplashScreen.Dispose() but that didn't work either (I thought maybe it would have to go after the initialization was done) .. the msgbox appears so I know it's being raised, but can't kill the darn app! Am I going to have to code it into the FormLoad event?

 

Thanks again! :-\

M.

 

 

Public Sub New()
  MyBase.New()

  '[MP]   Prevent Multiple Instances of the Application
  Dim appProcesses As Process() = _
     Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

  If appProcesses.Length > 1 Then
     MsgBox("Application.Exit Triggered")
     Application.Exit()
  End If

  '[MP]   Show Splash screen while the Application Loads
  Dim SplashScreen As New frmSplashScreen()
  SplashScreen.ShowDialog()
  SplashScreen.Update()

  '[MP]   Initialize the Application (Global Settings)
  Initialize(Me)

  'This call is required by the Windows Form Designer.
  InitializeComponent()

  'Add any initialization after the InitializeComponent() call
   SplashScreen.Dispose()

   End Sub

Posted

Yes, which is what is strange .. I get the msgbox which indicates that it's a second instance and should close .. yet, both Me.Close() and Application.Exit() both don't kill the app .. it runs as normal!?!

 

Very strange ..

 

M.

  • *Experts*
Posted

You could make a Sub Main to initialize your program with. For

example, set your startup object to Sub Main from the project

properties, and add this class:

Public Class Init
   Public Shared Sub Main()
       If findPreviousInstance() Then
           Application.Exit()
       Else
           Dim frm As New Form1()

           Application.Run(frm)
       End If
   End Sub

   Public Shared Function findPreviousInstance() As Boolean
       'do your check
   End Function
End Class

  • Moderators
Posted

I still can't get this to work... the value of appProcesses is always 0

Public Sub New()
  MyBase.New()

  Dim appProcesses As Process() = Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

  If appProcesses.Length > 1 Then
     MsgBox("Triggered")
  End If
  InitializeComponent()
End Sub

Visit...Bassic Software
Posted

Hey Robby,

 

Create a class module named "Initialize" and replace all code with the following:

 

Option Explicit On 


Public Class Initialize

  Shared Sub Main()

  '[MP]   Prevent Multiple Instances of the Application
  Dim appProcesses As Process() = _
  Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
  If appProcesses.Length > 1 Then
     Application.Exit()
  Else
     Dim Main As New frmMain()
     Application.Run(Main)
  End If

End Sub

 

Set your startup project as Sub Main() and replace 'frmMain' with whatever your main form is called.

 

This works .. I'm running it without problems! If you still have problems, then I'll post a sample app and you can try that ...

 

M.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...