Hi!
I need such:
When my program starting it must find ether other copy of the program presents in the system or no.
If it presents the program must maximize it and exit.
How can I do so?
Thanks for help,
Alex.
Printable View
Hi!
I need such:
When my program starting it must find ether other copy of the program presents in the system or no.
If it presents the program must maximize it and exit.
How can I do so?
Thanks for help,
Alex.
-Use the System.Diagnostics.Process class to get a list of running processes by name, handle, or window handle. Or if you prefer, inspect all window titles to find your process.
-Look out to catch System.Diagnostics.Win32Exception
-Determine which instance of your program to send messages to by inspecting the process start time. (Check the 'Ticks' property of DateTime. That should be more accurate)
-Use Process.CloseMainWindow() or Process.Kill() to exit whichever instance you need to exit.
OK, all excellent, but I cannot remove other problem:
1) I have hidden program (Form.Hide())
2) I have found it
3) I try show it by
int hWnd = localByName[nIndex].MainWindowHandle.ToInt32();
Win32API.SetForegroundWindow(hWnd);
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
where all is simple calling of API functions.
But it works only on minimized window, not hidden.
It looks as it try but the form know that it is hidden and hides again.
How can I set the form of other programm to Show()?
Thanks,
Alex.
I'm not familiar with the Win32 API so I really can't help (at least now). If I come across a solution I will be sure to post it.
But for one, don't convert obj.MainWindowHandle to int. Your Win32 API function should have a prototype as
[DllImport("User32.dll")]
<whatever goes here> SetForegroundWindow(IntPtr hWnd);
The marshalling type for HWND is System.IntPtr
Same goes for ShowWindow(IntPtr hWnd......);