Single Instance Application...


Author
Message
Robin J Giltner
Robin J Giltner
StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)StrataFrame User (179 reputation)
Group: Forum Members
Posts: 105, Visits: 650
When using Strataframe the required Startup Object is AppMain to that the database connections for the Business Objects can be established, and with that, you can't use the Visual Studio Option of Single Instance Application in the Application Properties.  So I was wondering what was the best way of handling this manually.

I spent a few minutes reading through some ways people have done this online, but it seems to boil down to searching through the processes and finding one that matches your form's Title or something such.  I ended writing a quick method that searches on the type of the form in the AppMain's InitApplication method.

Is this the best way to handle this ?  Also this doesn't currently allow the same application to be run by multiple users on the same system, ie users over Terminal Services etc.  I guess I would have to add another quick check for if the Owner of that Process is the current Application's process owner or some such.

I was just wanting to get some input from anyone who may have had to do this as well.

Thanks

Robin Giltner


Reply
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Larry,



Thanks for the information. I google this issue and found some different approaches, I combined two of them and was able to put together a solution that will will do just what is expected from the user: if the application is minimized, it will just bring it to the from.



Of course this solution is not as simple as using the Mutex and may have other issues since it uses the System.Diagnostics.Process to get to the application.



Ok here is the code.. Copy these 2 classes to a new class file in the main project I called mine "SingleInstance.vb" or if you have a based class in the main .exe project you could use that as well.



#Region " Code to handle a single instance of the application "



'Windows API

Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long



'''

''' Function to check and see if an instance of the application is already open

'''


''' Name of the application

''' True/False

'''

Public Shared Function IsAlreadyOpen(ByVal sApp As String) As Boolean

'Check running processes to see if application is already running

Dim pProcess As Process() = System.Diagnostics.Process.GetProcessesByName(sApp)

If pProcess.Length > 1 Then 'If > 1 then its already running

Return True

Else 'Not running

Return False

End If

End Function



Public Shared Sub ActivatePrevInstance(ByVal argStrAppToFind As String)

Dim PrevHndl As Long

Dim result As Long



Dim objProcess As New Process 'Variable to hold individual Process

Dim objProcesses() As Process 'Collection of all the Processes running on local machine

objProcesses = Process.GetProcesses() ''Get all processes into the collection



For Each objProcess In objProcesses



If UCase(objProcess.ProcessName) = UCase(argStrAppToFind) Then

If Not String.IsNullOrEmpty(objProcess.MainWindowTitle) Then

'MessageBox.Show(objProcess.MainWindowTitle)

PrevHndl = objProcess.MainWindowHandle.ToInt32()

Exit For

End If

End If

Next

If PrevHndl = 0 Then Exit Sub 'if No previous instance found exit the application.

''If found

result = OpenIcon(PrevHndl) 'Restore the program.

result = SetForegroundWindow(PrevHndl) 'Activate the application.



End 'End the current instance of the application.

End Sub

#End Region





Now like in your case, to execute it in the AppMain.vb after the "Public Shared Sub Main()" add this code:



If SingleInstance.IsAlreadyOpen(My.Application.Info.ProductName) Then

SingleInstance.ActivatePrevInstance(My.Application.Info.ProductName)

Exit Sub

End If



Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search