473,434 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Single-instance app: how to force running instance window to foreground?

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?
Apr 3 '07 #1
6 4993
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...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jen" wrote:
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?
Apr 3 '07 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:04**********************************@microsof t.com...
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...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jen" wrote:
>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?

Apr 3 '07 #3
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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:04**********************************@microsof t.com...
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...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jen" wrote:
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?


Apr 3 '07 #4
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:76**********************************@microsof t.com...
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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
> 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:04**********************************@microso ft.com...
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...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jen" wrote:

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?



Apr 3 '07 #5
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
http://www.codeproject.com/csharp/oneProcessOnly.asp

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)...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:76**********************************@microsof t.com...
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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:04**********************************@microsof t.com...
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...

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jen" wrote:

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?
Apr 3 '07 #6
On Tue, 03 Apr 2007 11:28:08 -0700, Peter Ritchie [C# MVP]
<PR****@newsgroups.nospamwrote:
[...]
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
Apr 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
12
by: Dennis Plöger | last post by:
Hi all! I'm currently having some problems parsing a char array in c++. (And yes, I'm a half-newbie ;-)) Perhaps you can help me with this: #include <iostream> using std::cout; void...
3
by: Jason | last post by:
I have several tables with quite a few fields and I'm getting errors when trying to insert records with single quotes in the data like: name = John O'Henry or a city name of O'Fallen So I went...
11
by: Thom Little | last post by:
I would like three states on an icon ... Left Click Right Click Double Click Left Click is fired at least once on a Double Click Is there a good example that shows how to determine if the...
2
by: John Dann | last post by:
I'm retrieving some columns from a database with numeric values that fall comfortably within the range of the Single type and I'm tempted to use Single for the relevant column type in the retrieved...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
11
by: Elmo | last post by:
Hi all! I am not very proud to ask this but here is my problem: string code = "\'13\'" The string code will have to contain following info: '51','52','63','other'... to get certain info...
3
by: Eric Layman | last post by:
Hi, I've saved data into the db by doing a replace() on single quote. Right now on data display on a datagrid, it shows double single quote. How do I make changes during run time of datagrid...
2
by: Reporter | last post by:
I got the following example from http://www.evolt.org/article/User_Friendly_Forms_in_PHP/20/60144/index.html : echo '<tr><td>First name:</td><td><input type="text" name="first_name"...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.