Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

I need my program to know al the different instances running

I today made my program support multiple instances and want to add a "Next Instance" button, for example. This would allow the user to toggle through all instances running. BUt I need to know how to find a list of all such instances. Also, I need to be able to find where in that list the current instance exists so I can increment the next instance in the list.

Could someone please tell me how?

Thanks,
newbieweb
Avatar of elimesika
elimesika
Flag of Israel image

HI

If I understood you well , you are talking on instances of an program exe, is that true ?
SOLUTION
Avatar of elimesika
elimesika
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

This sounds interesting. How does it work?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does this:

System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath));

return an empty array when running inside the debugger? That's what I am getting, but expected a size of one.
Yep...when running in debug mode the app is hosted by visual studio so the actual exe would be something like:

    WindowsApplication1.vshost.exe

You could try this instead:

    System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
    string ProcessFileName = System.IO.Path.GetFileNameWithoutExtension(p.MainModule.FileName);
    System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(ProcessFileName);

But it doesn't make much difference since if you want more than one instance you couldn't be in debug mode in the first place...
I just wanted to debug the related code as if it were the only process running. Then I'd start a nother one first, then start the debugger to see how being the second process changed things.

Thanks!
newbieweb
Right...but if you're running one directly from the .exe and another in the IDE then they will have TWO different "process names" and couldn't be trapped by the same piece of code since the filenames will be different...

One would be "XXXX" and the other would be "XXXX.vshost".  So the second one, which is running in the IDE, will think it's the only one running still.