473,799 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding a running instance of my app

Hey all,

Maybe someone knows this problem; I have a C# WinForms app that every
now and then has a new version that I deploy to my users using an
installation script written with the NSIS installer. To improve on this,
I want the installer to shut down any running instances of my
application when it does the installation.

To do this, I have implemented the following function to kill any
instances I find (yeah, I have the pinvoke for PostMessage and the other
functions right):

(Sorry if the posted code comes out wrong)

System.Diagnost ics.Process[] myProcesses;
myProcesses = System.Diagnost ics.Process.Get ProcessesByName ("MyApp");
foreach (System.Diagnos tics.Process instance in myProcesses)
{
if( instance.MainWi ndowHandle == (System.IntPtr) 0 )
{
IntPtr h = IntPtr.Zero;
int tid;
int pid;
do
{
pid = 0;
// get the window handle
h = FindWindowEx(In tPtr.Zero, h, null, null);
// get the process id (and thread id)
tid = GetWindowThread ProcessId(h, out pid);
if (pid == instance.Id)
{
PostMessage( h, 0x11, 0, 0 );
break;
}
} while( !h.Equals(IntPt r.Zero) );
}
else
PostMessage( instance.MainWi ndowHandle, 0x11, 0, 0 );
}

This is partially based on something I found on the web.

It fine if the Window of the application is open - then
instance.MainWi ndowHandle is ok, and the shutdown message works fine (I
used this because this is a systray based app, closing the Window does
nothing, but 0x11 is what Windows uses to shut down the app when the
system is going down, I figure I can use this for my own shutdown as
well). So that is no problem. If the user minimizes the application, the
window is hidden - in that case, it loops trough all the windows, and
sends the 0x11 message to any windows it finds that belongs to the found
process ID.

However, one thing that doesn't work, is if the main window of the
application is closed, and other windows are open - then
MainWindowHandl e will return 0, AND the code above will not find any
windows owned by the process. Thus I can not post the message, and
nothing will happen.

Having the main window of my app closed while other of it's windows are
open isn't an uncommon situation with this program, so my question is -
does anyone know what to do? Why is the window not "existing" and owned
by the correct process when another window has been opened?

Any insight will be highly appreciated!

Thanks,

Rune
Jan 6 '06 #1
3 5938
Rune,

I wouldn't do it this way at all.

What I would do is make a call to RegisterWindowM essage in your
application. This will allow you to pass a string to the function, and get
a windows message id which is unique throughout the system.

You would store this in your app, and then override the WndProc method
to check against that message id. If you get it, you simply exit the
application (calling Application.Exi t should do the trick).

Then, in your update program, you would call PostMessage, passing the
HWND_BROADCAST constant for the window handle. This will cause the message
to be sent to all top level windows. Other applications will ignore the
message, because they dont know how to interpret it (assuming that they play
nice and call RegisterWindows Message to create their message ids). Your
app, however, will know what to do.

You just have to call RegistrWindowMe ssage in your update app with the
same string to indicate the message to send.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rune Jacobsen" <rune.jacobsen@ no_spam.broadpa rk.no> wrote in message
news:43******** @news.broadpark .no...
Hey all,

Maybe someone knows this problem; I have a C# WinForms app that every now
and then has a new version that I deploy to my users using an installation
script written with the NSIS installer. To improve on this, I want the
installer to shut down any running instances of my application when it
does the installation.

To do this, I have implemented the following function to kill any
instances I find (yeah, I have the pinvoke for PostMessage and the other
functions right):

(Sorry if the posted code comes out wrong)

System.Diagnost ics.Process[] myProcesses;
myProcesses = System.Diagnost ics.Process.Get ProcessesByName ("MyApp");
foreach (System.Diagnos tics.Process instance in myProcesses)
{
if( instance.MainWi ndowHandle == (System.IntPtr) 0 )
{
IntPtr h = IntPtr.Zero;
int tid;
int pid;
do
{
pid = 0;
// get the window handle
h = FindWindowEx(In tPtr.Zero, h, null, null);
// get the process id (and thread id)
tid = GetWindowThread ProcessId(h, out pid);
if (pid == instance.Id)
{
PostMessage( h, 0x11, 0, 0 );
break;
}
} while( !h.Equals(IntPt r.Zero) );
}
else
PostMessage( instance.MainWi ndowHandle, 0x11, 0, 0 );
}

This is partially based on something I found on the web.

It fine if the Window of the application is open - then
instance.MainWi ndowHandle is ok, and the shutdown message works fine (I
used this because this is a systray based app, closing the Window does
nothing, but 0x11 is what Windows uses to shut down the app when the
system is going down, I figure I can use this for my own shutdown as
well). So that is no problem. If the user minimizes the application, the
window is hidden - in that case, it loops trough all the windows, and
sends the 0x11 message to any windows it finds that belongs to the found
process ID.

However, one thing that doesn't work, is if the main window of the
application is closed, and other windows are open - then MainWindowHandl e
will return 0, AND the code above will not find any windows owned by the
process. Thus I can not post the message, and nothing will happen.

Having the main window of my app closed while other of it's windows are
open isn't an uncommon situation with this program, so my question is -
does anyone know what to do? Why is the window not "existing" and owned by
the correct process when another window has been opened?

Any insight will be highly appreciated!

Thanks,

Rune

Jan 6 '06 #2

"Rune Jacobsen" <rune.jacobsen@ no_spam.broadpa rk.no> wrote in message
news:43******** @news.broadpark .no...
Hey all,

Maybe someone knows this problem; I have a C# WinForms app that every now
and then has a new version that I deploy to my users using an installation
script written with the NSIS installer. To improve on this, I want the
installer to shut down any running instances of my application when it
does the installation.

To do this, I have implemented the following function to kill any
instances I find (yeah, I have the pinvoke for PostMessage and the other
functions right):

(Sorry if the posted code comes out wrong)

System.Diagnost ics.Process[] myProcesses;
myProcesses = System.Diagnost ics.Process.Get ProcessesByName ("MyApp");
foreach (System.Diagnos tics.Process instance in myProcesses)
{
if( instance.MainWi ndowHandle == (System.IntPtr) 0 )
{
IntPtr h = IntPtr.Zero;
int tid;
int pid;
do
{
pid = 0;
// get the window handle
h = FindWindowEx(In tPtr.Zero, h, null, null);
// get the process id (and thread id)
tid = GetWindowThread ProcessId(h, out pid);
if (pid == instance.Id)
{
PostMessage( h, 0x11, 0, 0 );
break;
}
} while( !h.Equals(IntPt r.Zero) );
}
else
PostMessage( instance.MainWi ndowHandle, 0x11, 0, 0 );
}

This is partially based on something I found on the web.

It fine if the Window of the application is open - then
instance.MainWi ndowHandle is ok, and the shutdown message works fine (I
used this because this is a systray based app, closing the Window does
nothing, but 0x11 is what Windows uses to shut down the app when the
system is going down, I figure I can use this for my own shutdown as
well). So that is no problem. If the user minimizes the application, the
window is hidden - in that case, it loops trough all the windows, and
sends the 0x11 message to any windows it finds that belongs to the found
process ID.

However, one thing that doesn't work, is if the main window of the
application is closed, and other windows are open - then MainWindowHandl e
will return 0, AND the code above will not find any windows owned by the
process. Thus I can not post the message, and nothing will happen.

Having the main window of my app closed while other of it's windows are
open isn't an uncommon situation with this program, so my question is -
does anyone know what to do? Why is the window not "existing" and owned by
the correct process when another window has been opened?

Any insight will be highly appreciated!


I have seen many examples of how to implement a single instance application
and they all use remoting to look for the running instance and tell it what
to do. Of course in this case they pass it a new file or some such but they
could just as easily tell it to shut down.

Can't remember the details because I don't use remoting much but it was v.
v. simple

Jan 6 '06 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Rune,

I wouldn't do it this way at all.

What I would do is make a call to RegisterWindowM essage in your
application. This will allow you to pass a string to the function, and get
a windows message id which is unique throughout the system.

[snip]
Hope this helps.


It did indeed! Thanks for very valuable input! The application now shuts
down no matter what kind of state it is in when the message comes!

You just earned yourself a "thank you" in the about box! :)

Rune
Jan 8 '06 #4

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

Similar topics

2
1697
by: Skip Montanaro | last post by:
At work we have a package structure for our homegrown code. Developers each have their own sandboxes where they work on the stuff they are responsible for (mixtures of C++ libraries, SWIG wrappers and pure Python packages). We're running into a problem where we have two versions of the main package, one in the developer's sandbox that contains just the developer's bits and one in the central location which contains all the installed stuff....
5
1650
by: Scott McNair | last post by:
Hi, I'm writing an ASP page that will analyze the logfiles and return statistical information. I'd like to make this app so that I could drop it into any of my webservers and get the logs for that server. Is there a way to determine dynamically the location of the logfiles? Regards, Scott
1
4100
by: Bruce | last post by:
I am writing an installer that needs to put a dll file with a bunch of extended stored procedures in the BINN directory. The problem is that if the machine is running multiple instances of SQL Server, there is more than one BINN directory. The installer has a dynamically populated dropdown with the name of the instances, so does anyone know if this logic is correct and/or reliable assuming SQL Server 2000? If the instance name =...
3
1857
by: Tim | last post by:
Hello Everyone I'd like to create a script/program that can locate all servers running DB2, finding out which instances are running/created and what databases are running under them but have been told but IBM support that this isn't currently possible for a client to do. I'd like to get a second opinion since the CCA seems to be able to manage it without any problems so the API must be there to do it. The main reason why I'd like...
3
4707
by: John Veldthuis | last post by:
I am trying to find instances of all the SQL servers on the local network including the machine it is running from. The servers will be a mix of SQL2000 and SQL2005 once it is ready. I have been trying it out with the PC on which the program is running also has SQL Server 2005 running on it at the same time but the code will not pick this instance up. I call the following on the Form Load Imports Microsoft.SqlServer.Management.Smo ...
3
2606
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to user to inform him that only one instance is permit and then close the second instance after that. I am able to do this when the user run the application on his PC whit this : Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName) ...
3
12746
by: Rico | last post by:
Hello, I have a generic process that logs errors from different sources. When I call this code, I'd like to also submit the name of the function or sub that is raising the error without having to manually type this in for each instance. For instance, (air code to follow) Public Sub LogMyError( errNumber as int, errDescription as string, SubFuncName as string)
7
9084
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've tried several different approaches for finding the placeholder: This: MyControl = this.FindControl("$form$tabs_repeater$PlaceHolder1"); Produces this error: Object reference not set to an instance of an object.
7
1695
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
9544
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
10259
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
10030
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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
5467
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
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.