Hi Alex,
My mistake, i forgot that Control.FromHandle() only works on handles
belonging to the current process as "Windows" maintains a window table on a
per process basis. So in order to bring your other window to the front you
will have to use some form of Inter Process Communication like windows
messages to communicate to your other instance. I found this article that
uses P/Invoke to bring the previous instance window to the foreground ,
check it out,
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Hi. Okay well, I still can't seem to solve this problem. I really
appreciate
your help. Here's my entire Main(). It's very short. Can you help me
figure
out what I'm doing wrong? THANKS! ...
[STAThread]
static void Main()
{
Process[] ProcessArr =
System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName
);
Process AlreadyRunningProcess;
//Will never be GREATER THAN 2 by this logic...
// 1 process was the original...
// Process 2 is the one just starting now
if(ProcessArr.Length > 1)
{
//I've tried both [0] AND [1] below
AlreadyRunningProcess = ProcessArr[0];
Control AlreadyRunningMainWind =
Control.FromHandle(AlreadyRunningProcess.MainWindowHandle);
// For text purposes, I've done the next 2 lines.
// Returns NULL string...
string tmpStr = AlreadyRunningProcess.ProcessName;
MessageBox.Show("ProcessName = " + tmpStr);
// AlreadyRunningMainWind is always NULL for some reason!
if(AlreadyRunningMainWind == null)
MessageBox.Show("Window Handle is NULL!");
else
AlreadyRunningMainWind.Focus();
}
else
{
Application.Run(new RunOnlyOnceTestFrm());
}
}
:
Ok here's a thought, when you run the second instance of your
application
isn't that also going to have the same process name and get enumerated
in
the call to GetProcessByName() so actually you should be checking that
Length > 2, try it. Now you need to correctly identify the already
running
process from your current process. That can be done by comparing the
process
handles of your current process and the list returned by the
GetProcessByName() method(). Can you also post some sample code if you
still
have problems.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Hi. You know what? I think I liad before when I said I already had a
reference to the running Process. It's starnge, I'm getting back an
array
of
Processes from my
".Process.GetProcessesByName()" call and I'm using the array length to
determine whether the application is already running. Length = 1, this
is
the
first one running. Length > 1, it's already running. Now here's where
I go
wrong: If I do:
"AlreadyRunningProcess = ProcessArr[0];" where ProcessArr[] was
already
filled from my call to ".Process.GetProcessesByName()", what I get
back
doesn't seem to work right. If I then try even the simplest
"AlreadyRunningProcess.MachineName" or anything else, I get null
values
back.
I've also tried using "AlreadyRunningProcess = ProcessArr[1];"
thinking
that
maybe the order of process returned would be backward but NEITHER of
these
works.
Can you help further?
Thanks!
:
Give this a shot,
Once you get the Process associated with your application
Control mainWindow = Control.FromHandle(myProcess.MainWindowHandle);
//Then try either
mainWindow.BringToFront();
//Or
mainWindow.Focus();
//Or
mainWindow.Show();
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
message
How, in .NET Windows Forms applications, can I create an app
where, if
the
user double-clicks or otherwise attempts to start the EXE when
it's
already
running, it simply brings the already-running application to the
front
(standard Windows UI approach). I have figured out how to use the
Process
object to find out if this application is already running and I
can
get va
reference to the already-running Process. But how do I make that
Process's
application window/windows come to the front, etc.? And if it
can't be
done
because of inter-application security restrctions or some-such,
how
does
Microsoft do it?
Thanks for your help!
Alex