M
Mark Phillips
Hello,
I am having a problem in which a file can get stuck open when a thread
that is attempting to write to it gets aborted (ThreadAbortedException
occurs). The log file gets stuck open until the process is shut down
(cannot delete file from Windows Explorer, for example). My test
application stops execution with: An unhandled exception of type
'System.IO.IOException' occurred in mscorlib.dll Additional
information: The process cannot access the file "c:\abort_test.txt"
because it is being used by another process.
I figured I was not closing things properly, but I think I'm OK. I
created a simple test application and am still stumped. Here's my code
(added to a visual studio.net 2003 Windows application):
public static void ThreadProc()
{
// Just keep writing to the log file until thread gets aborted
for (; true
{
try
{
using ( FileStream fs = File.Open(@"c:\abort_test.txt",
FileMode.Append,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString()+" A line of text");
}
}
}
catch (ThreadAbortException e)
{
using ( FileStream fs = File.Open(@"c:\abort_test2.txt",
FileMode.Append,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString()+ e.Message + "--" +
e.StackTrace);
}
}
}
Thread.Sleep(0);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Sometimes code aborts in non-harmful places, so loop enough
times to almost guarantee a problem
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(1000);
t.Abort();
t.Join();
}
}
Here's the contents from my c:\abort_test2.txt:
11/14/2003 3:43:02 PMThread was being aborted.-- at
Microsoft.Win32.Win32Native.CreateFile(String lpFileName, Int32
dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES
securityAttrs, FileMode dwCreationDisposition, Int32
dwFlagsAndAttributes, IntPtr hTemplateFile)
at Microsoft.Win32.Win32Native.UnsafeCreateFile(String lpFileName,
Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES
securityAttrs, FileMode dwCreationDisposition, Int32
dwFlagsAndAttributes, IntPtr hTemplateFile)
at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean
useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess
access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess
access)
at TestFileAbortProblem.Form1.ThreadProc() in
c:\programs\dotnet\testing\testfileabortproblem\testfileabortproblem\form1.cs:line
98
Any ideas on how I can make sure the file gets closed or how I can
prevent the ThreadAbortException exception from being thrown during
file manipulation?
Thanks in advance,
Mark
I am having a problem in which a file can get stuck open when a thread
that is attempting to write to it gets aborted (ThreadAbortedException
occurs). The log file gets stuck open until the process is shut down
(cannot delete file from Windows Explorer, for example). My test
application stops execution with: An unhandled exception of type
'System.IO.IOException' occurred in mscorlib.dll Additional
information: The process cannot access the file "c:\abort_test.txt"
because it is being used by another process.
I figured I was not closing things properly, but I think I'm OK. I
created a simple test application and am still stumped. Here's my code
(added to a visual studio.net 2003 Windows application):
public static void ThreadProc()
{
// Just keep writing to the log file until thread gets aborted
for (; true
{
try
{
using ( FileStream fs = File.Open(@"c:\abort_test.txt",
FileMode.Append,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString()+" A line of text");
}
}
}
catch (ThreadAbortException e)
{
using ( FileStream fs = File.Open(@"c:\abort_test2.txt",
FileMode.Append,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString()+ e.Message + "--" +
e.StackTrace);
}
}
}
Thread.Sleep(0);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Sometimes code aborts in non-harmful places, so loop enough
times to almost guarantee a problem
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(1000);
t.Abort();
t.Join();
}
}
Here's the contents from my c:\abort_test2.txt:
11/14/2003 3:43:02 PMThread was being aborted.-- at
Microsoft.Win32.Win32Native.CreateFile(String lpFileName, Int32
dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES
securityAttrs, FileMode dwCreationDisposition, Int32
dwFlagsAndAttributes, IntPtr hTemplateFile)
at Microsoft.Win32.Win32Native.UnsafeCreateFile(String lpFileName,
Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES
securityAttrs, FileMode dwCreationDisposition, Int32
dwFlagsAndAttributes, IntPtr hTemplateFile)
at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean
useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess
access, FileShare share)
at System.IO.File.Open(String path, FileMode mode, FileAccess
access)
at TestFileAbortProblem.Form1.ThreadProc() in
c:\programs\dotnet\testing\testfileabortproblem\testfileabortproblem\form1.cs:line
98
Any ideas on how I can make sure the file gets closed or how I can
prevent the ThreadAbortException exception from being thrown during
file manipulation?
Thanks in advance,
Mark