Hi,

I have encountered a problem. I am checking if my application "testApp" is found in the ApplicationTab in TaskManager. The code below works 100%.

But the problem is this when you just "rightclick" with the mouse on the "testApp" in the Taskbar, this code will produce the file and tell that it no longer was found in the ApplicationTab.

This happens only when you "rightclick" the "testApp" in the Taskbar without doing anything else.
I wonder why this happens as I need to check that "testApp" is still running all the time?

      using System.Diagnostics;
        private void checkConnection_DoWork(object sender, DoWorkEventArgs e)
        {

            while (true)
            {
                bool applicationFound = false;
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    String getName = clsProcess.MainWindowTitle.ToString().ToLower();
                    if (getName.IndexOf("testapp") != -1)
                    {
                        applicationFound = true; break;
                    }
                }

                if (applicationFound == false)
                {
                    FileStream hr = new FileStream("C:/test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamWriter file1 = new StreamWriter(hr); 
                    file1.WriteLine("Process not found..."); file1.Close(); hr.Close();
                }
                Thread.Sleep(1000);
            }
        }

Edited 4 Years Ago by Darth Vader

How about trying a different approach by searching the currently running processes for a specific window title?

Process process = null; 
foreach (Process p in Process.GetProcessesByName("iexplore")) 
{ 
    if (p.MainWindowTitle.Contains("My Test Application")) 
    { 

        break; 
    } 
} 

Thanks for answer,

The exact same scenario happens in this case. As I really have seen is that the "testApp" dissapears from the list of processes for a very very short while when you "rightclick" on the applicatoin in the taskbar.
The short while could be for about 0.25 seconds or so. After this it does exist in the processes again.

This is a really big problem that I dont understand why this is happening. It almost feels lika Windows operativesystem bug in somehow. It doesn´t make any sense why it dissapear for this very short while when only "rightclicking" the application in the taskbar.

Using the below code will reproduce the scenario. I use this code to be absolutetly sure that a process is still running which is essential as it handles important resources but with this behaviour it just give me a big Questionmark what is happening and how I can be sure to know that the process is running?

        using System.Diagnostics;
        private void checkConnection_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                bool applicationFound = false;
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    String getName = clsProcess.MainWindowTitle.ToString().ToLower();
                    if (getName.IndexOf("testapp") != -1)
                    {
                        applicationFound = true; break;
                    }
                }

                if (applicationFound == false)
                {
                    FileStream hr = new FileStream("C:/test.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                    StreamWriter file1 = new StreamWriter(hr); 
                    file1.WriteLine("Process not found..."); file1.Close(); hr.Close();
                }
                Thread.Sleep(1);
            }
        }

Does the Process Identifier (PID) number in task manager change for your testapp whenever it disappears and then reappears after approximately a quarter of a second?

I did a test to check the ID of the process. I did rightclick many times and the (PID) number in taskmanager remains the same. It doesn´t take a new ID. So it is still unique then. As in this case it was: "2924"

Still I wonder how to solve this problem? I have googled on everything but nothing found about this particular problem, which is a big problem indeed.

This article has been dead for over six months. Start a new discussion instead.