473,772 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5019
There's nothing in the Framework to do what you want, you'll have to PInvoke
SetForegroundWi ndow. You can use the Process.MainWin dowHandle property with
SetForegroundWi ndow...

--
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 AllowSetForegro undWindow function to allow itself to
be brought to the front.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:04******** *************** ***********@mic rosoft.com...
There's nothing in the Framework to do what you want, you'll have to
PInvoke
SetForegroundWi ndow. You can use the Process.MainWin dowHandle property
with
SetForegroundWi ndow...

--
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 SetForegroundWi ndow is the current foreground
process it should be able to call SetForegroundWi ndow without
AllowSetForegro undProcess being called. If the application calling
SetForegroundWi ndow 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 AllowSetForegro undProcess.

--
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 AllowSetForegro undWindow function to allow itself to
be brought to the front.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:04******** *************** ***********@mic rosoft.com...
There's nothing in the Framework to do what you want, you'll have to
PInvoke
SetForegroundWi ndow. You can use the Process.MainWin dowHandle property
with
SetForegroundWi ndow...

--
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 SetForegroundWi ndow
before the application loop starts (which means he has no window).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:76******** *************** ***********@mic rosoft.com...
If the application calling SetForegroundWi ndow is the current foreground
process it should be able to call SetForegroundWi ndow without
AllowSetForegro undProcess being called. If the application calling
SetForegroundWi ndow 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 AllowSetForegro undProcess.

--
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 AllowSetForegro undWindow function to allow itself
to
be brought to the front.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:04******* *************** ************@mi crosoft.com...
There's nothing in the Framework to do what you want, you'll have to
PInvoke
SetForegroundWi ndow. You can use the Process.MainWin dowHandle property
with
SetForegroundWi ndow...

--
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 SetForegroundWi ndow in .NET
that make no use of AllowSetForegro undWindow:
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 SetForegroundWi ndow
before the message pump starts:
[STAThread]
static void Main ( )
{
Process[] myProcesses =
Process.GetProc essesByName(Pro cess.GetCurrent Process().Proce ssName);
if(myProcesses != null && myProcesses.Len gth 1)
{
foreach (Process process in myProcesses)
{
if (process != Process.GetCurr entProcess())
{
UnsafeNativeMet hods.SetForegro undWindow(proce ss.MainWindowHa ndle);
return;
}
}
}
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
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 SetForegroundWi ndow always works if
a process is attempting a call on SetForegroundWi ndow 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 SetForegroundWi ndow
before the application loop starts (which means he has no window).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:76******** *************** ***********@mic rosoft.com...
If the application calling SetForegroundWi ndow is the current foreground
process it should be able to call SetForegroundWi ndow without
AllowSetForegro undProcess being called. If the application calling
SetForegroundWi ndow 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 AllowSetForegro undProcess.

--
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 AllowSetForegro undWindow function to allow itself
to
be brought to the front.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Ritchie [C# MVP]" <PR****@newsgro ups.nospamwrote in message
news:04******** *************** ***********@mic rosoft.com...
There's nothing in the Framework to do what you want, you'll have to
PInvoke
SetForegroundWi ndow. You can use the Process.MainWin dowHandle property
with
SetForegroundWi ndow...

--
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****@newsgro ups.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 SetForegroundWi ndow always works if
a process is attempting a call on SetForegroundWi ndow 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 SetForegroundWi ndow 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
SetForegroundWi ndow. 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 SetForegroundWi ndow 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
8262
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 understand.)
12
5127
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 outchar(char *outcharstring)
3
3101
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 ahead and added a replace to replace the ' with " but now other fields are having the same problem and there are multiple fields involved. This data gets into having a lot of symbols used, etc. So rather than go through and replace these quotes...
11
535
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 user entered a Left Click or a Double Click?
2
9765
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 dataset. (Actually the values are held as integers in the database for compact storage but are scaled to their true Single values during retrieval). But I'm sure that I keep reading that there's no performance penalty to using Double rather...
11
2257
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 Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
11
1870
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 from the database. When the querry is parsed these values will be looked up:
3
2743
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 so that the double single quote will be replaced as single quote? Pls advise.
2
2886
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" value="'.htmlspecialchars($_POST).'"></td></tr>'; What purpose do the single quotes serve in value="'.htmlspecialchars($_POST).'" I don't mean the single quotes around first_name. I mean the single
0
8632
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6713
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.