Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

File IO and Threading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jayme Pechan - 06 Oct 2006 20:09 GMT
I seem to have run into a strange problem.  I'm hoping one of you understand
the inner working of the file IO better than I and could explain why I'm
seeing what I am.  Basically, I have a log folder where I append to text
files frequently.  Every 10 minutes or so, I check to see if the files are
greater than a certain size and then archive them.  After archiving the
files, I use the File.Delete function to delete each of the files.  I put
the File.Delete in a lock section to ensure that it completes before another
threat attempts to write to them.  The problem I'm seeing is that it seems
like the File.Delete doesn't delete the files immediately and even though it
is supposed to remove it from the directory tree (I think), if another
thread attempts to append to one of these files too soon after leaving my
lock block, the whole program locks up.  I thought of putting in a sleep
after the deletions but I wouldn't know how long to set it for.

My solution is to move the files to a sub folder first before deleting them
but wanted to see if anyone would know why I'm having this problem in the
first place.

Thanks

Jayme
Sean Chambers - 07 Oct 2006 20:59 GMT
A better way would be to rename the file first, like logfile.log.old.

This way if another thread tries to write, it will recreate the file as
the file doesn't exist, then shortly thereafter the logfile.log.old is
deleted.

Would this work?

If not, try putting a 10ms sleep, then 20ms, all the way up until you
find the correct amount of time to sleep for, although I would
recommend against putting the thread to sleep. I don't usually use this
method as it feels kind of clunky to me.

Sean

> I seem to have run into a strange problem.  I'm hoping one of you understand
> the inner working of the file IO better than I and could explain why I'm
[quoted text clipped - 17 lines]
>
> Jayme
Michael D. Ober - 07 Oct 2006 22:03 GMT
Can we see your code.  At least the writes and deletes.

Thanks,
Mike Ober.

> A better way would be to rename the file first, like logfile.log.old.
>
[quoted text clipped - 32 lines]
> >
> > Jayme
Gaurav Vaish (www.EdujiniOnline.com) - 08 Oct 2006 04:58 GMT
> like the File.Delete doesn't delete the files immediately and even though
> it is supposed to remove it from the directory tree (I think), if another

Are you sure that when you are deleting the file, the file is not already
opened?

If the file is already opened, File.Delete will not throw any exception but
the file will be deleted only when it is closed.

The method is nothing but a wrapper around the DeleteFile method in Win32
API (see url below).

   http://windowssdk.msdn.microsoft.com/en-gb/library/ms685330.aspx

Pay attention to the 'Remarks' section:

[quote]
The DeleteFile function marks a file for deletion on close. Therefore, the
file deletion does not occur until the last handle to the file is closed.
[/quote]

Hope that helps...

Signature

Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujinionline.com
http://eduzine.edujinionline.com
-----------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.