At the moment I'm trying to build a singleton application without using the "Single Instance" flag, this is due to some of the mechanisms of the application actually requires multiple instances (self updater,etc). So I have to build my own method of insuring that there is only one instance of the application.
I've gotten about halfway through creating this, I've gotten to the point of
- Detects how many instances are running
- A method to show the other instances Main Window (this is where I'm stuck)
The problem is the application most of the time runs in the background, hidden, with no item in the taskbar. When calling process.MainWindowHandle, it always returns 0, as for that function to detect the current "MainWindow" it requires the window to be A) Visible and B) Showing in taskbar.
Is there anyway around this limitation?
A method I can think of, but have no idea of implementing is to store the MainWindowHandle the first time the application is visible, but how would i expose this value?
Current code:
Dim running_processes As Process() = Process.GetProcessesByName("helpdesk")
Dim current_process_id As Integer = Process.GetCurrentProcess().Id
If (running_processes.Length = 1) Then
'Run the app like normal
bootstrap_loader.Show()
Else
For Each process As Process In running_processes
If process.Id = current_process_id Then Continue For
'MainWindowHandle returns 0 when window is not visible
'Sidenote: ShowWindow is from user32.dll :)
ShowWindow(process.MainWindowHandle, SHOW_WINDOW.SW_NORMAL)
'Exit the application like a baws
'Environment.Exit(2)
Next
End If