Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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
0
newbieweb
Asked:
newbieweb
  • 3
  • 3
  • 2
2 Solutions
 
elimesikaCommented:
HI

If I understood you well , you are talking on instances of an program exe, is that true ?
0
 
elimesikaCommented:
anyway, if that is the case , you can use a simple manager class that invokes the exe using System.Diagnostic.Process.Start method this method  returns  a System.Diagnostics.Process class , so your manager should have a Dictionary<string,System.Diagnostics.Process> in which you store the processes , the first process may be called "1" , the second "2" etc. and then you can get the process from the dictionary by its name.
0
 
newbiewebSr. Software EngineerAuthor Commented:
This sounds interesting. How does it work?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...you can get an array of Process() that holds all of your running instances with:

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

If you want to manipulate the window then use "ps[x].MainWindowHandle".  For instance, you could pass that to the SetForegroundWindow() API.
0
 
newbiewebSr. Software EngineerAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
newbiewebSr. Software EngineerAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now