RE: Bug in FileStream.Dispose in Framework 1.1
- From: v-phuang@xxxxxxxxxxxxxxxxxxxx ("Peter Huang" [MSFT])
- Date: Wed, 14 Sep 2005 05:33:13 GMT
Hi
We have confirmed this is problem in our product, and we will plan to fix
it in the next release(Whidbey).
So you may try to use CloseHandle API to force the file handle release if
you found there is a IOException with error(disk is full)
This is just test code, you may adjust according or your scenario.
NOTE: Please try to get the FileHandle before the exception may occur.
class Class1
{
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string path = @"..\..\Test1.txt";
string str = "Test";
byte[] bts = Encoding.ASCII.GetBytes(str);
//byte[] bts = new byte[512*1024*1024];
MessageBox.Show("Before Create");
FileStream fs = new FileStream(path,FileMode.OpenOrCreate);
IntPtr hFile = fs.Handle;
try
{
MessageBox.Show("Created");
while(true)
fs.Write(bts,0,bts.Length);
fs.Flush();
MessageBox.Show("Before Closed");
fs.Close();
MessageBox.Show("Closed");
}
catch(Exception ex)
{
try
{
MessageBox.Show(ex.ToString());
fs.Flush();
MessageBox.Show("Before Closed");
fs.Close();
MessageBox.Show("Closed");
}
catch(Exception exp)
{
try
{
MessageBox.Show(exp.ToString());
MessageBox.Show("Before Closed");
fs.Close();
MessageBox.Show("Closed");
}
catch(Exception expt)
{
MessageBox.Show(expt.ToString());
}
}
}
MessageBox.Show("Before Exit");
//MessageBox.Show(CloseHandle(hFile).ToString());//We have to call the
API to force close the file handle
fs = null;
MessageBox.Show("Before GC");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
MessageBox.Show("After GC");
}
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- References:
- Bug in FileStream.Dispose in Framework 1.1
- From: Dathon
- RE: Bug in FileStream.Dispose in Framework 1.1
- From: "Peter Huang" [MSFT]
- RE: Bug in FileStream.Dispose in Framework 1.1
- From: "Peter Huang" [MSFT]
- Bug in FileStream.Dispose in Framework 1.1
- Prev by Date: RE: Outlook RSS Feed Add-In, Required .NET Framework, Fails with e
- Next by Date: Re: Hosting CLR in VB6 using COM
- Previous by thread: RE: Bug in FileStream.Dispose in Framework 1.1
- Next by thread: RegEx - Is this doable? In .NET
- Index(es):
Relevant Pages
|