473,569 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

One application instance with hidden window scenario

Hello.

I have an application which I check to see if there is another running
instance at startup. If there is another running instance, I set
focus to the existing instance. I am using code that I found on a
webpage to do this (Code I am using is at the bottom of this post).
When the application is minimized, the form is hidden and an icon
appears in the System Tray. My problem is that when I run another
instance, the new instance attempts to set focus to the hidden window.
The window does not reappear. Can I modify the code to not only make
sure that the window is in focus, but to Show the window as well?

Thanks.
-Joe

CODE:

[STAThread]
static void Main()
{
//Get the running instance.
Process instance = RunningInstance ();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new frmMain());
}
else
{
//There is another instance of this process.
HandleRunningIn stance(instance );
}
}

public static Process RunningInstance ()
{
Process current = Process.GetCurr entProcess();
Process[] processes = Process.GetProc essesByName
(current.Proces sName);

//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetEx ecutingAssembly ().Location.Rep lace("/", "\\") ==
current.MainMod ule.FileName)
{
//Return the other process instance.
return process;
}
}
}

//No other instance was found, return null.
return null;
}
public static void HandleRunningIn stance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainW indowHandle , WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWi ndow (instance.MainW indowHandle);
}

[DllImport("User 32.dll")]
private static extern bool ShowWindowAsync (
IntPtr hWnd, int cmdShow);
[DllImport("User 32.dll")] private static extern bool
SetForegroundWi ndow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
Nov 16 '05 #1
0 1482

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

Similar topics

6
4647
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and if second one is loaded it must be close and give focus to first one. Thanks for any advice! Dmitry
6
4276
by: Ollie Riches | last post by:
I understand the use of Application.DoEvents() to process all outstanding messages on the message queue in a winforms application if you have long running process on the UI thread. But can anyone explain to me why I need to call DoEvents when I am using a COM component that calls back to into the ..Net application? - If I don't call DoEvents...
3
4599
by: kiran | last post by:
How can I check whether a application is laready running in the current system...? I want to check when the application is in "hidden" state. first instance window style is "Hide" . When ever user trying to open a second or new instance .it should detect the first instance automatically and first instance should be activate and show to the...
6
5281
by: Max | last post by:
I have the following code on a form that launches Microsoft Outlook and creates a new email message for the user: Outlook.Application oApp = new Outlook.Application(); Outlook.MailItem oMail = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); oMail.HTMLBody = "SOME HTML TEXT HERE"; oMail.Display(oApp); ...
6
11201
by: Charles Neitzel | last post by:
I'm trying to write a windows application in C# (Using Microsoft Visual C# 2005 Express) that is nothing more than a simple UI with buttons on it. The buttons do various things like running programs and executing registry entries. The majority of my buttons work however, I have come upon a problem. I need a few of the buttons to run DOS batch...
8
2811
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance; /// <summary> /// public property that can only get the single instance of this class. /// </summary>
0
1452
by: midlothian | last post by:
I have some long Excel routines that I would like to run from my database in a hidden, second instance of the same Access db. The macro I pass to the shell command to open the second instance contains code to 'Hide' the database (from Dev's site) and also the code which runs the Excel routine. It then quits the database when the code has...
7
2217
by: Joe | last post by:
Is it possible to have a component which is global to the entire application? I need to have a single component act sort of like a server which components in any of the forms can access. For example if I drop a component on Form1 & Form2 and that component has a property called Server, at design time I would like to be able to assign the...
28
7352
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). I have created a windows service from VStudio 2005 template. What is the windows service replacement for Application.Run()?
0
7694
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...
0
7609
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...
0
7921
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. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
0
3651
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...
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.