Question identify from the list which process is not running

shawnv

New member
Joined
May 27, 2015
Messages
4
Programming Experience
Beginner
Hi,

I have created an application to check if our application process is running. Because there is more than one instance of this process running (6 processes) with the same name, the only way to identify which is down is to look at the start up parameter in the CommanLine from SELECT CommandLine FROM Win32_Process. The fist start up parameter is the directory which is used to uniquely identify which one is down.

So what I am trying to do is to loop using foreach and in my app.config have list where i can identify from the list which process is not running. But it does exactly what foreach does. All i want is to see which String of the site is not in the loop.

<add key="sites" value="D:\site,D:\site1,D:\site2"/>
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%{1}'", proc, ".exe");


            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
            ManagementObjectCollection retObjectCollection = searcher.Get();


            Console.WriteLine(retObjectCollection.Count);


            if (retObjectCollection.Count < 6)
            {


                foreach (ManagementBaseObject retObject in retObjectCollection)
                {
                    string cmdLine = retObject["CommandLine"].ToString();
                    string leftStr = cmdLine.Substring(0, 30);
                    string shortLeftStr = leftStr.Substring(0, 14);
                    
                    Console.WriteLine(shortLeftStr );


                    foreach(string site in sites)
                         {
                                if (site.startswith(shortLeftStr))
                                    {
                                          console.writeline(site + " is up and running");
                                     }
                                 else
                                     {
                                            console.writeline(site + " is Down");
                                      }
                          }
                }

              }


Your help will be much appreciated.

:welcoming:
 
Last edited by a moderator:
This is what comes of writing code without thinking through the logic that it's supposed to implement first. Think about how you would do this if you had to do it manually rather than in code. Firstly, you'd start by looking through the list of the processes that should be running, not the list of those that are running. That means that you have your loops backwards. So, for each site listed in your config file, you would then start looking down the list of processes that ARE running. You would keep looking down the list until you either find a match, in which case you would say that it IS running, or you get to the end of the list, in which case you would say that it's NOT running.
 
Thanks jmcilhinney,

I have changed to :
foreach (string site in sites)
                {
                    foreach (ManagementBaseObject retObject in retObjectCollection)
                    {
                        string cmdLine = retObject["CommandLine"].ToString();
                        string leftStr = cmdLine.Substring(0, 30);
                        string shortLeftStr = leftStr.Substring(0, 14);


                        if (site != shortLeftStr)
                        {
                            Console.WriteLine(site + " Process Not running!");
                        }


                    }
                }

Is this what you meant?
 
Last edited by a moderator:
Firstly, I had already fixed the formatting of your code snippet in post #1 and you then posted another unformatted code snippet. Please format your code snippets for readability.

As for the question, does that code actually implement the logic I described?
 
Thanks again, new to this forum. Not sure how to format the code section. I will read up on how to do it. The code is giving me what I am looking for just the Processes from the sites list which are not running but i get double up records,

The process AKC and DNC is running the other 4 are down.

Output example:

D:\compass\AKC Process Not running!
D:\compass\CHC Process Not running!
D:\compass\CHC Process Not running!
D:\compass\cim Process Not running!
D:\compass\cim Process Not running!
D:\compass\DNC Process Not running!
D:\compass\PNC Process Not running!
D:\compass\PNC Process Not running!
D:\compass\WNC Process Not running!
D:\compass\WNC Process Not running!

Again, thanks fir the help. Much appreciated.
 
It concerns me greatly that you can't see that that code doesn't do what you want and why. If you can't do this stuff in your head then don't try. Actually write it down. Pick up a pen and paper if needs be but by all means type it into a file. Write out the steps that you need to perform in plain English or whatever your native language might be. Once you have the whole process broken down into the most fundamental steps possible you write corresponding pseudo-code. Once you've confirmed that the pseudo-code does indeed match you're algorithm, then you write actual code. That way, you're not plucking code out of thin air and guessing whether it actually does what you want.

Let's look at the logic again. You want to loop through the processes that should be running first, so your first loop is OK. You want to loop through the processes that are running next, so your second loop is OK. It's what you do when you compare that is completely wrong. When you compare the current item from the outer to the current item from the inner loop they either match or they don't. If they do then you want to note that and break out of the inner loop. If they don't match then you simply continue. Once you get out of the loop, you look at whether you found a match or not and notify the use accordingly. It should be obvious that you use a Boolean for that; true means you found a match and false means you didn't. By default you have not found a match, so you initialize it to false, setting it to Tue if and when you find a match.
 
Thanks, I have come right. I have set a flag in the inner to break to the outer to continue with processes expected to run. Works like a charm.

Try not to break peoples spirit especially if they are new to programming. After your first paragraph I had to crack the code and did it.

 
Back
Top Bottom