By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

File stuck open after ThreadAbortException

P: n/a
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(Strin g 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\te stfileabortproblem\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
Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.