0

I have a TextBox which tells the status of a running application (lets say notepad). If notepad is running Text of TextBox is running and not running for other case.

public string ProcessStatus
{
    get 
    {
         IsProcessRunning("Notepad.exe")
              return "Running";
         return "Not Running";
    }
}

Now problem here is that view updates itself only once when it is launched. At that time if notepad is running it works fine. Now lets suppose I ran my application and notepad was not running then TextBox says not running. Now I launch notepad, now application is still saying not running as application has not updated the view. If I call notify of property changed event for the TextBox then it will say running. But I want here is that TextBox updates automatically.

The only solution what I am thinking right now is that I start a background process which keeps on updating ProcessStatus. But is this the right way? Is there any better way? Something like DirectoryWatcher for processes?

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • I think the culprit is the calling code. The UI thread is probably blocked by your polling mechanism or something. – Gerrie Schenck Sep 11 '13 at 09:29
  • http://stackoverflow.com/questions/8455873/how-to-detect-a-process-start-end-using-c-sharp-in-windows and http://stackoverflow.com/questions/1986249/c-sharp-process-monitor – S_F Sep 11 '13 at 09:31

3 Answers3

2

You could use a System.Windows.Threading.DispatcherTimer to check at regular intervals:

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(10000); // checks every 10 seconds
timer.Tick += Timer_Tick;
timer.Start();

...

private void Timer_Tick(object sender, EventArgs e)
{
    // do your checks here
    textbox.Text = ProcessStatus;
}

You can find out more about the DispatcherTimer class from the DispatcherTimer Class page at MSDN.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

Why not use the timer class to periodically run ProcessStatus, you can define the interval.

Nathan Smith
  • 8,271
  • 3
  • 27
  • 44
0

On this other question Can I Get Notified When Some Process Starts? there are two answers on how you can get notified if a process (e.g. Notepad.exe) starts. Both are neither ideal nor simple, I would probably stick to polling as Sheridan and NSmeef suggested.

Community
  • 1
  • 1