keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp > 04/2007

 #1  
04-03-07, 05:47 PM
Jen
I've implemented single-instance functionality in my .exe by using the mutex
method. Works great. But when the .exe detects that it is not the first
instance I want to bring the main window of the first instance to the
foreground and set focus to it before the .exe exits. Is this possible
using pure .NET calls or do I need Windows API calls and what are they?
 #2  
04-03-07, 06:14 PM
Peter Ritchie [C# MVP]
There's nothing in the Framework to do what you want, you'll have to PInvoke
SetForegroundWindow. You can use the Process.MainWindowHandle property with
SetForegroundWindow...
 #3  
04-03-07, 06:21 PM
Nicholas Paldino [.NET/C# MVP]
On top of that, the original application will have to make a call
through P/Invoke to the AllowSetForegroundWindow function to allow itself to
be brought to the front.
 #4  
04-03-07, 06:32 PM
Peter Ritchie [C# MVP]
If the application calling SetForegroundWindow is the current foreground
process it should be able to call SetForegroundWindow without
AllowSetForegroundProcess being called. If the application calling
SetForegroundWindow isn't the foreground process (e.g. it doesn't have a
window) then yes, the other application must give permission to the specific
process via AllowSetForegroundProcess.
 #5  
04-03-07, 06:49 PM
Nicholas Paldino [.NET/C# MVP]
Well, if the OP is trying to prevent a second instance of his app from
starting, then in the second process, he would call SetForegroundWindow
before the application loop starts (which means he has no window).
 #6  
04-03-07, 07:28 PM
Peter Ritchie [C# MVP]
There's actually a couple of examples of using SetForegroundWindow in .NET
that make no use of AllowSetForegroundWindow:
http://www.codeproject.com/vb/net/Ac...select=1101700
[url down]

I came up with a short example that attempts to call SetForegroundWindow
before the message pump starts:
[STAThread]
static void Main ( )
{
Process[] myProcesses =
Process.GetProcessesByName(Process.GetCurrentProce ss().ProcessName);
if(myProcesses != null && myProcesses.Length > 1)
{
foreach (Process process in myProcesses)
{
if (process != Process.GetCurrentProcess())
{
UnsafeNativeMethods.SetForegroundWindow(process.Ma inWindowHandle);
return;
}
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}

It seems to always bring up the other application (I only tried 10 times; so
"always" is subjective).

Could be my concept of "foreground process" (in that it has the foreground
window) is flawed. Or, it could be that SetForegroundWindow always works if
a process is attempting a call on SetForegroundWindow on another instance of
the same application and simply isn't documented. Definately a difference in
documentation though (at least my interpretation of it)...
 #7  
04-03-07, 10:04 PM
Peter Duniho
On Tue, 03 Apr 2007 11:28:08 -0700, Peter Ritchie [C# MVP]
<PRSoCo> wrote:

> [...]
> Could be my concept of "foreground process" (in that it has the
> foreground window) is flawed.


That's probably the case. I'm not actually aware of any official
definition of "foreground process", though obviously there must be one for
the documentation to make any sense. However, it seems to me that
"foreground process" could easily include simply the process that was just
started when no other changes to focus has been made. After all, if the
process starting up isn't already the foreground process when it creates
its first window, it's hard to see how the rules would allow its window to
wind up the foreground window. :)

> Or, it could be that SetForegroundWindow always works if
> a process is attempting a call on SetForegroundWindow on another
> instance of the same application and simply isn't documented.


It would be easy enough to test for that condition. Just write a second
application that uses SetForegroundWindow on the first. If indeed there's
a special case for the process being the same application, then using a
second application will result in a failure to set the first application
as the foreground application.

If it works, then that would indicate that indeed a process can be the
foreground process without actually having a window.

Of course, all of this said, IMHO it's a bad idea _generally_ to use
SetForegroundWindow. There's a reason that the rules are complex and
there are many ways for it to not work. That is, it's rude to the user to
bring a new process to the foreground without the user's explicit
instructions or consent.

IMHO, it's almost always better to use SetActiveWindow instead. I think
that in the situation described by the OP the SetForegroundWindow is more
appropriate, but I hope anyone reading through this thread will note that
it's a very specific situation and the solution does not apply to all
other situations.

Pete
Similar Threads
can't bring "single instance" tray app to foreground

I'm using the single instance class found at <http://www.codeproject.com/threads/simplesingleinstanceapp.asp>. When the first instance of my app receives the notification...

Running a single instance of an app

Hi, I am running a background process (Console App) on a WM5 Pocket PC. I'd like to have just one instance of the process running at the same time. Something as giving the...

More on single-instance apps: how to force its window to foregroun

Although the app I'm working on is a multi-threaded java client application based on Eclipse, using SWT, the issue at hand is purely Windows related. The app works with text...

Custom Taglib problems - instead of a single instance per page, I have a single instance per application.

Hi, What is the correct expected behaviour when using a taglib regarding how many objects are created per page/application? More clearly, I created a custom tag (call it...

Force 1 Instance of IE Window on URL

I have a web app. that I only want 1 instance of it on the workstation for the user. I have users that will load this app., minimize it, forget it's minimized and then...


All times are GMT. The time now is 04:27 AM. | Privacy Policy