-
September 15th, 2009, 12:12 PM
#1
File.Delete File.Move Only Works in Debug
This is an odd one.
With all the guts out I have this:
TextReader tr = new StreamReader("List.txt");
TextWriter tw = new StreamWriter("List2.txt");
//Guts
tw.Close();
tr.Close();
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
When I run this I get an error of:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'List.txt' because it is being used by another process.
Yet.. When I debug and go through it step by step, it works perfectly.. Odd.. any clues?
Cheers.
-
September 15th, 2009, 12:27 PM
#2
Re: File.Delete File.Move Only Works in Debug
Oh Dear... there was a typo in one of the earlier tw, tr's. And this didnt kick up an error! Oops.
Shame there isnt a 'Remove Post' option Sorry. Still.... very odd how it would work while stepping through.. hmm..
-
September 15th, 2009, 12:47 PM
#3
Re: File.Delete File.Move Only Works in Debug
Originally Posted by demise
Oh Dear... there was a typo in one of the earlier tw, tr's. And this didnt kick up an error! Oops.
Shame there isnt a 'Remove Post' option Sorry. Still.... very odd how it would work while stepping through.. hmm..
It works stepping though because of the slower timing present while debugging.
-
September 15th, 2009, 01:11 PM
#4
Re: File.Delete File.Move Only Works in Debug
The problem is simply that the TextReader and TestWriter objects haven't been disposed.
Just wrap them in a couple of using blocks.
Code:
using( TextReader tr = new StreamReader("List.txt") )
{
using( TextWriter tw = new StreamWriter("List2.txt") )
{
//Guts
}
}
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
In a general sense, in my opinion, there aren't any 'timing' issues in Windows. Instead there are [lack of] synchronization issues.
In other words, if you view these sorts of problems as 'timing' related, you'll always have trouble because things happen differently in different environments. Instead view it as a synchronization issue and figure out what you need to do to synchronize the operations properly. In this case, it's just adding the using block so you know the file handles are released after leaving the using block scope (and before the Delete and Move operations are run).
-
September 16th, 2009, 03:56 AM
#5
Re: File.Delete File.Move Only Works in Debug
Originally Posted by Arjay
The problem is simply that the TextReader and TestWriter objects haven't been disposed.
Just wrap them in a couple of using blocks.
Code:
using( TextReader tr = new StreamReader("List.txt") )
{
using( TextWriter tw = new StreamWriter("List2.txt") )
{
//Guts
}
}
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
In a general sense, in my opinion, there aren't any 'timing' issues in Windows. Instead there are [lack of] synchronization issues.
In other words, if you view these sorts of problems as 'timing' related, you'll always have trouble because things happen differently in different environments. Instead view it as a synchronization issue and figure out what you need to do to synchronize the operations properly. In this case, it's just adding the using block so you know the file handles are released after leaving the using block scope (and before the Delete and Move operations are run).
Great bit of info there, Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|