If your want to ensure that only a single instance of your application may be executed at any given time, then use a Mutex object,
which can be shared across-process boundaries as a singleton:
private static Mutex SingleInstanceMutex;
/// <summary>Disposes the mutex object that ensures only a single instance of this application will execute at any given
time.</summary>
private static void SingleInstanceProcess_Exited(object sender, EventArgs e)
{
if (SingleInstanceMutex != null)
SingleInstanceMutex.Close();
}
public static void Main()
{
Process process = Process.GetCurrentProcess();
bool createdNew;
SingleInstanceMutex = new Mutex(true, process.ProcessName, out createdNew);
if (createdNew)
process.Exited+= new EventHandler(SingleInstanceProcess_Exited);
else
// Allow only a single instance of the app to execute
process.Kill();
}
So if a window is hidden, it seems not to have a windowhandle.
Incorrect. I attempted to reproduce the behavior your experiencing and I've done so with success. Take this code for example:
CheckHiddenHandleForm form = new CheckHiddenHandleForm();
Console.WriteLine("Show Form...");
form.Show();
System.Windows.Forms.Application.DoEvents();
Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Console.WriteLine("\tForm.Handle: {0}", form.Handle);
Console.WriteLine("Hide Form...");
form.Hide();
System.Windows.Forms.Application.DoEvents();
Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Console.WriteLine("\tForm.Handle: {0}", form.Handle);
Console.WriteLine("Close Form...");
form.Close();
System.Windows.Forms.Application.DoEvents();
Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Outputs the following results to the Console window:
Show Form...
MainWindowHandle: 5308896 -- MainWindowHandle
Form.Handle: 5308896 -- Notice that the Form.Handle is the MainWindowHandle
Hide Form...
MainWindowHandle: 0 -- there is no longer a Main window for the process since the Form has been hidden
Form.Handle: 5308896 -- *** The window still has a handle ***
Close Form...
MainWindowHandle: 0 -- The window is now disposed (Form.Handle is now inaccessible and assumed
IntPtr.Zero)
Press any key to continue
As you can see, the window still has a handle, however, once it is hidden it is no longer the "MainWindow" of the process. You may
have to use Windows API calls to obtain the handle to the window, or just ignore it because it's hidden. That's what you're
attempting to accomplish anyway, correct?
I suggest looping through all of the processes with the same name if you do not want to ensure a single instance of the app using
the Mutex in my code example above. If the process Id is not of the current process, AND the MainWindowHandle is not IntPtr.Zero
then use my Interop code I supplied to you in a previous post to show the window.
GL
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Mészáros Tamás" <me*****@sch.bme.hu> wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...
I just looked at your code to access the process that you posted
earlier. It seems your attempting to acquire the current process:Process p=Process.GetCurrentProcess();
Why not just use Process.GetCurrentProcess().MainWindowHandle?
I don't understand how that is useful. Isn't your code running in
the main form?
I use GetCurrentProcess( ).Processname to determine the name of the process. Then I look for other running processes whit the same
name. If any other process exists whit the same name, then I would like to set that other window visible, and close this one.
So I don't want to allow to run two instances from the same application. When I try to execute the second one, I would like to set
the first one visible, and exit the second one.
It's sure, it exists, I've only called a Hide() method on it, and
hWnd is 0 (I've tried it).What did you try? A handle of 0 (zero) means that no window exists.
(period).
I started the application, then started an other instance of it, and the second one could realize the windowhandle of the first
one. Then I hid the first app's main window (with Form.Hide( )), looked for the window handle of it, but it was 0. I've tried
"third party" applications as well, and they found the handle to bo 0 as well. So if a window is hidden, it seems not to have a
windowhandle.
Tamas