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 (ThreadAbortedE xception
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.IOEx ception' 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.Wri te))
{
using (StreamWriter sw = new StreamWriter(fs ))
{
sw.WriteLine(Da teTime.Now.ToSt ring()+" A line of text");
}
}
}
catch (ThreadAbortExc eption e)
{
using ( FileStream fs = File.Open(@"c:\ abort_test2.txt ",
FileMode.Append ,FileAccess.Wri te))
{
using (StreamWriter sw = new StreamWriter(fs ))
{
sw.WriteLine(Da teTime.Now.ToSt ring()+ e.Message + "--" +
e.StackTrace);
}
}
}
Thread.Sleep(0) ;
}
}
private void Form1_Load(obje ct sender, System.EventArg s 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(Thr eadProc));
t.Start();
Thread.Sleep(10 00);
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.Cr eateFile(String lpFileName, Int32
dwDesiredAccess , FileShare dwShareMode, SECURITY_ATTRIB UTES
securityAttrs, FileMode dwCreationDispo sition, Int32
dwFlagsAndAttri butes, IntPtr hTemplateFile)
at Microsoft.Win32 .Win32Native.Un safeCreateFile( String lpFileName,
Int32 dwDesiredAccess , FileShare dwShareMode, SECURITY_ATTRIB UTES
securityAttrs, FileMode dwCreationDispo sition, Int32
dwFlagsAndAttri butes, IntPtr hTemplateFile)
at System.IO.FileS tream..ctor(Str ing path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean
useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileS tream..ctor(Str ing 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 TestFileAbortPr oblem.Form1.Thr eadProc() in
c:\programs\dot net\testing\tes tfileabortprobl em\testfileabor tproblem\form1. cs:line
98
Any ideas on how I can make sure the file gets closed or how I can
prevent the ThreadAbortExce ption 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 (ThreadAbortedE xception
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.IOEx ception' 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.Wri te))
{
using (StreamWriter sw = new StreamWriter(fs ))
{
sw.WriteLine(Da teTime.Now.ToSt ring()+" A line of text");
}
}
}
catch (ThreadAbortExc eption e)
{
using ( FileStream fs = File.Open(@"c:\ abort_test2.txt ",
FileMode.Append ,FileAccess.Wri te))
{
using (StreamWriter sw = new StreamWriter(fs ))
{
sw.WriteLine(Da teTime.Now.ToSt ring()+ e.Message + "--" +
e.StackTrace);
}
}
}
Thread.Sleep(0) ;
}
}
private void Form1_Load(obje ct sender, System.EventArg s 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(Thr eadProc));
t.Start();
Thread.Sleep(10 00);
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.Cr eateFile(String lpFileName, Int32
dwDesiredAccess , FileShare dwShareMode, SECURITY_ATTRIB UTES
securityAttrs, FileMode dwCreationDispo sition, Int32
dwFlagsAndAttri butes, IntPtr hTemplateFile)
at Microsoft.Win32 .Win32Native.Un safeCreateFile( String lpFileName,
Int32 dwDesiredAccess , FileShare dwShareMode, SECURITY_ATTRIB UTES
securityAttrs, FileMode dwCreationDispo sition, Int32
dwFlagsAndAttri butes, IntPtr hTemplateFile)
at System.IO.FileS tream..ctor(Str ing path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean
useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileS tream..ctor(Str ing 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 TestFileAbortPr oblem.Form1.Thr eadProc() in
c:\programs\dot net\testing\tes tfileabortprobl em\testfileabor tproblem\form1. cs:line
98
Any ideas on how I can make sure the file gets closed or how I can
prevent the ThreadAbortExce ption exception from being thrown during
file manipulation?
Thanks in advance,
Mark