tags:

views:

355

answers:

2

I'm trying to find out when a process has stopped doing his work. I've been trying it with this code but it doesn't notice that the program is still running and processing a file. Probably because it's still doing things that take less then one microsecond:

    TimeSpan startTime = m_Process.TotalProcessorTime;
    int idleCycles = 0;
    int iMax = Math.Max(iMinNoActivityTime/100, 5);
    while (idleCycles < iMax)
    {
        Sleep(100);
        TimeSpan curTime = m_Process.TotalProcessorTime;
        int delta = curTime.Subtract(startTime).Milliseconds;
        if (delta != 0)
        {
          idleCycles = 0;
        }
        else
        {
            idleCycles++;
        }
        startTime = curTime;
    }

It's called for 3000 seconds: 30 consecutive time-blocks of 100 miliseconds without processor activity.

Is there any way to do this so it doesn't see it as idling when it's still running? The process reads the file, deletes it and then processes it so it can't monitor the directory.

+1  A: 

I'd go back and look at your design here. Trying to spot when another process is idle doesn't seem like a very solid approach.

Why not just have the other process use a named event to signal when it is done?

GrahamS
A: 

If you know that the work is being done on the main (UI) thread, for example for a simple console application, then you can use Process.WaitForInputIdle. You can optionally specify a timeout parameter if you just want to poll. Now, if the work is being done on a background thread, then you're not going to be able to detect it (at least not without resorting to some nasty hacks). As GrahamS points out, it's certainly better to rethink design in thise case.

Noldorin