mpappert Posted December 10, 2002 Posted December 10, 2002 Hey Everyone! I want to prevent users from creating multiple instances of my application, instead have it set focus to the original instance (in VB6 you could use the App.PrevInstance method, as shown in the the MS article below). MS Knowledge Base Article :: Q185730 Is there a .NET equivilent? Thanks! M. Quote
*Experts* Volte Posted December 10, 2002 *Experts* Posted December 10, 2002 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 Quote
mpappert Posted December 10, 2002 Author Posted December 10, 2002 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. Quote
*Gurus* Derek Stone Posted December 10, 2002 *Gurus* Posted December 10, 2002 I'm not aware of any method of setting focus to other windows outside your application without using the Win32 API. Quote Posting Guidelines
Moderators Robby Posted December 10, 2002 Moderators Posted December 10, 2002 This returns the correct string as expected.. Diagnostics.Process.GetCurrentProcess.ProcessName but the rest does not. Quote Visit...Bassic Software
*Experts* Volte Posted December 10, 2002 *Experts* Posted December 10, 2002 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. Quote
Moderators Robby Posted December 10, 2002 Moderators Posted December 10, 2002 I think you may need to write to the Eventlog for this to work. Quote Visit...Bassic Software
mpappert Posted December 11, 2002 Author Posted December 11, 2002 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. Quote
*Gurus* Derek Stone Posted December 11, 2002 *Gurus* Posted December 11, 2002 Application.Exit() Quote Posting Guidelines
mpappert Posted December 11, 2002 Author Posted December 11, 2002 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 Quote
Moderators Robby Posted December 11, 2002 Moderators Posted December 11, 2002 I'm curious, is the MsgBox("Application.Exit Triggered") even triggering? Quote Visit...Bassic Software
mpappert Posted December 11, 2002 Author Posted December 11, 2002 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. Quote
*Experts* Volte Posted December 11, 2002 *Experts* Posted December 11, 2002 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 Quote
mpappert Posted December 11, 2002 Author Posted December 11, 2002 It Works!!!! Thanks everyone for your help! M. :D Quote
Moderators Robby Posted December 11, 2002 Moderators Posted December 11, 2002 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 Quote Visit...Bassic Software
mpappert Posted December 12, 2002 Author Posted December 12, 2002 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.