1

Hello guys,

I created a thread that scanning for some files on the desktop and do some actions with that. The problem is the process of this thread happens only once - when the scanning of files done.

My purpose with that thread is to scan directories anytime without stopping but without creating an overflow of CPU usage. I want to use a smart way to prevent stopping of this scanning.

The action with the files is to check if there is a specific content that need to be there, I am scanning for this content in all files.

I tried to use the while infinite loop style:

public void bwScanning()
{
   while(true)
   {
       // the whole code of scanning goes here.
   }
}

But It's too risky because of the pumping system resources with that loop.

I thought about a few things how to create smarter code:

  1. Run a thread by timer with delay of 20 seconds. But then, i don't know what is the amount of files and how much time takes for the process of scanning dirs to be done..

  2. maybe create number of threads - after the first thread finished, create a new one. I think it's very ridiculous to use this way because of the new creation and memory usage.

Some people that encountered with the same problem can advise me?

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Daniel Chernenkov
  • 715
  • 1
  • 7
  • 23
  • 11
    I don't know what you need to do with the files you find but it sounds like you might want to look into [`FileSystemWatcher`](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) – Dave Zych Oct 02 '13 at 19:33
  • 1
    @DaveZych ok, i will read about that. – Daniel Chernenkov Oct 02 '13 at 19:35
  • 1
    Have you looked into `Thread.Sleep()` to pause the thread. I am not sure what you mean by _pumping system resources with that loop_? – hylander0 Oct 02 '13 at 19:37
  • Maybe this question can help: [Limit the number of Tasks in Task.Factory.Start by second](http://stackoverflow.com/questions/18771524/limit-the-number-of-tasks-in-task-factory-start-by-second) ? – L.B Oct 02 '13 at 21:04

3 Answers3

1

If you want to periodically scan the files but you do not know how long it takes something real simple is to put a sleep at the end of your while loop like this:

public void bwScanning()
{
   while(true)
   {
       // the whole code of scanning goes here.
       Thread.Sleep(20000); // sleep 20sec
   }
}

Another option is to use a timer to run your scan function in periodic intervals and keep a flag indicating whether you are still in the process of scanning and simply return if you are:

bool isProcessing;
public void bwScanning()
{
    if (isProcessing) return;
    isProcessing = true;
    try {
        // the whole code of scanning goes here.
    }
    finally { 
        // in case your processing code throws an exception this ensures you are resetting the flag
        isProcessing = false;
    }
}
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
0

A simple thread.sleep(1) should cut it, I have a software with several such loops running at once and a simple 1 ms sleep was enough to send the cpu usage back to under 1%.

Samuel
  • 710
  • 5
  • 8
0

How dow you stop the thread? Assumed you use an event object to signal shutdown, I'd suggest something like:

public void bwScanning()
{
   while( ! stopEvent.WaitOne(20*1000,false))
   {
       // the whole code of scanning goes here.
   }
}
JensG
  • 13,148
  • 4
  • 45
  • 55