473,287 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

FileSystemWatcher at 99% cpu

Hi,

I am using FileSystemWatcher to track the change in a text file. Whenever I
run the program I get 99% CPU utilization. The code doesn't do anything just
sits and waits in a while loop until the event is fired. Any ideas? Thanks

Frank
Apr 12 '06 #1
7 4457
Frank <a-******@microsoft.com> wrote:
I am using FileSystemWatcher to track the change in a text file. Whenever I
run the program I get 99% CPU utilization. The code doesn't do anything just
sits and waits in a while loop until the event is fired. Any ideas? Thanks


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '06 #2
Frank,

That's probably part of the problem. Why are you sitting in a while
loop? Are you doing anything else in the loop?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank" <a-******@microsoft.com> wrote in message
news:ur****************@TK2MSFTNGP03.phx.gbl...
Hi,

I am using FileSystemWatcher to track the change in a text file. Whenever
I run the program I get 99% CPU utilization. The code doesn't do anything
just sits and waits in a while loop until the event is fired. Any ideas?
Thanks

Frank

Apr 12 '06 #3
I figured it out. It seemes that the while loop is the fault. I changed the
while(true) to use Console.ReadLine() which helped.

using System.IO;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

static bool bNew = false;

static void Main(string[] args)

{

FileSystemWatcher buildWatcher = new FileSystemWatcher();

buildWatcher.Path = "C:\\";

buildWatcher.NotifyFilter = NotifyFilters.LastWrite

| NotifyFilters.FileName;

buildWatcher.Filter = "myfile.txt";

buildWatcher.Changed += new FileSystemEventHandler(OnChanged);

while (true)

{

if (bNew)

{

//. something to do here

}

}

}

public static void OnChanged(object source, FileSystemEventArgs e)

{

bNew = true;

}

}

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Frank <a-******@microsoft.com> wrote:
I am using FileSystemWatcher to track the change in a text file. Whenever
I
run the program I get 99% CPU utilization. The code doesn't do anything
just
sits and waits in a while loop until the event is fired. Any ideas?
Thanks


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 12 '06 #4
Frank <a-******@microsoft.com> wrote:
I figured it out. It seemes that the while loop is the fault. I changed the
while(true) to use Console.ReadLine() which helped.


Yes - the while(true) is certainly where the trouble is. You should
never, ever put a tight loop in your code like this - it will always
give this kind of result.

Now, why don't you want to do the work directly when the event handler
is called?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '06 #5
Just as an FYI, here's my code for something similar:

private void btnStartMonitoring_Click(object sender, EventArgs e)
{
System.IO.FileSystemWatcher fsw = new
FileSystemWatcher(txtDirectoryName.Text.ToString() );
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Filter = "*.cnk";
fsw.Created += new FileSystemEventHandler(OnCreated);
// Begin watching.
fsw.EnableRaisingEvents = true;
}

private static void OnCreated(object source, FileSystemEventArgs e)
{
string sMessage = "File: " + e.FullPath + " " + e.ChangeType;
MessageBox.Show(sMessage);
}

There is no loop monitoring the status; it simply fires when the event
occurs. Now one thing I'm still fighting with is that I need to Invoke the
process to happen afterwards, otherwise the UI does goofy things. See my
post from 04/08/06. But that's just tweaking... :) Famous last words.

Clint
"Frank" <a-******@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
I figured it out. It seemes that the while loop is the fault. I changed the
while(true) to use Console.ReadLine() which helped.

using System.IO;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

static bool bNew = false;

static void Main(string[] args)

{

FileSystemWatcher buildWatcher = new FileSystemWatcher();

buildWatcher.Path = "C:\\";

buildWatcher.NotifyFilter = NotifyFilters.LastWrite

| NotifyFilters.FileName;

buildWatcher.Filter = "myfile.txt";

buildWatcher.Changed += new FileSystemEventHandler(OnChanged);

while (true)

{

if (bNew)

{

//. something to do here

}

}

}

public static void OnChanged(object source, FileSystemEventArgs e)

{

bNew = true;

}

}

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Frank <a-******@microsoft.com> wrote:
I am using FileSystemWatcher to track the change in a text file.
Whenever I
run the program I get 99% CPU utilization. The code doesn't do anything
just
sits and waits in a while loop until the event is fired. Any ideas?
Thanks


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Apr 12 '06 #6
I am using console application, so I need to stay in some kind of a loop for
the program not to exit. I probably could move the work load into the event
handler but checking errors to determine when to exit the program. The
reason that I didn't use the Console class is that it sits there waiting for
input from the user when the program really is autonomous running in the
background.

Frank
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Frank <a-******@microsoft.com> wrote:
I figured it out. It seemes that the while loop is the fault. I changed
the
while(true) to use Console.ReadLine() which helped.


Yes - the while(true) is certainly where the trouble is. You should
never, ever put a tight loop in your code like this - it will always
give this kind of result.

Now, why don't you want to do the work directly when the event handler
is called?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 12 '06 #7
Frank <a-******@microsoft.com> wrote:
I am using console application, so I need to stay in some kind of a loop for
the program not to exit. I probably could move the work load into the event
handler but checking errors to determine when to exit the program. The
reason that I didn't use the Console class is that it sits there waiting for
input from the user when the program really is autonomous running in the
background.


Okay, a few things:

1) You probably don't actually need it to be a console app. Unless
you're *using* the console, it would probably look better as a WinForms
app which just doesn't bring up any forms - that way you wouldn't have
a blank window up for no reason.

2) If you need to capture multiple events, you could use a
producer/consumer queue, where the file system event "produces" an item
which is then consumed by the main loop.

3) If you only need to capture a single event, you can just use
Monitor.Wait/Pulse.

See http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for more
information on these (from half way down).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
5
by: Goran Djuranovic | last post by:
Hi all, I have a file system watcher service that works fine on a local hard drive, but will not work across the network. I tried both: mapping the drive and "\\..." path both no luck. I don't...
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.