By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

Reading the first file from a directory.

P: n/a
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig
Mar 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Sounds like what you need is a variation of the dir command switches.

From the command line, you can do:

dir /od

To sort by the data timestamp and then you could pick off just the first
file. Maybe DirectoryInfo will let you sort by date timestamp.
Craig Vermeer wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig

--
Get the new
http://www.you-read-it-here-first.com
content from Texeme Textcasting Technology
Mar 23 '06 #2

P: n/a
John Bailo wrote:

Sounds like what you need is a variation of the dir command switches.

From the command line, you can do:
You could use this:

http://www.shanebauer.com/Weblog/Per...888a11433.aspx

I'm assuming you are doing a FIFO queue so getting the earliest date
would work for you.

dir /od

To sort by the data timestamp and then you could pick off just the first
file. Maybe DirectoryInfo will let you sort by date timestamp.
Craig Vermeer wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file
system queue gets all that large (any more than a thousand or so
messages in the queue).

I know why. It's because my code to grab a single message off the
queue looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get
the first file in the directory.

One approach I can think of is to do some in-memory caching of the
list of files, so that I don't read them from disk every time.
However, there's a fair amount of effort involved there, because my
program reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig


--
Get the new
http://www.you-read-it-here-first.com
content from Texeme Textcasting Technology
Mar 23 '06 #3

P: n/a
Hi Craig,

have you considered having a service that uses FileSystemWatcher to maintain
a listing of the files in your directory?
The list could be as simple as a text file in that directory through to any
level of sophistication you might like to best fit your overall applciation
architecture.
It would at least allow you to go directly to a single point for your next
file.

Good Luck
Gerard
"Craig Vermeer" wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig

Mar 23 '06 #4

P: n/a
I would be inclined to use a combination of a FileSystemWatcher component
and a Queue object.

At application startup, start the FileSystemWatcher to wtach for new files
in your target folder, then use the GetFiles method and enqueue each
filename to the Queue object.

When your application wants to get a message from the queue simply

- Check the length of the Queue object (the number of entries)

- Dequeue the first entry into a string (filename)

- Check for the existence of the file

- Process it if it exists

The check for the existence of the file will cater for the potential for the
same filename to get put in the Queue object twice if the FileSystemWatcher
happens to fire bewteen the time you start it and the time you execute the
GetFiles method.

In my view it is best to do it that way round because if you do it the other
way round the is the potential for a file to be missed of it is created
between executing the GetFiles method and starting the FileSystemWatcher.
"Craig Vermeer" <ve******@sentdotcom.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I have a program that's using the file system as a queuing mechanism, and
it's consuming an inordinate amount of CPU time when the file system queue
gets all that large (any more than a thousand or so messages in the
queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it wants
to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list of
files, so that I don't read them from disk every time. However, there's a
fair amount of effort involved there, because my program reading the
messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know of
a very efficient way to just pull a single file from a directory?

Thanks,
Craig

Mar 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.