File cannot be accessed because used by another process

P

Parrot

I am creating a file in my WIndows based C# program with the following command:
FileStream fs1 = new FileStream(filename, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sr = new StreamWriter(fs1);

I then write records to the file and then close both the FileStream and the
Streamwriter with the following commands:

sr.Close();
sr.Dispose();
fs1.Close();
fs1.Dispose();

Later in the program I try to create the file again. 50% of the time I get
an error message saying the the file is currently being used by another
process. Can anyone tell me why I get this error and how I can get around
this error?

Dave
 
P

Parrot

Peter;
Thanks for your reply. I did as you suggested and it still refuses to
release the file to me. It goes through the loop 10 times and is unable to
delete the file because it says it is being used by another process. Below is
my loop to delete the file.

int x1 = 0;

while (x1 < 10)
{
{
try
{
if (ff.Exists)
ff.Delete();
else
{

x1 = 10;
break;
}
}


catch (Exception ex)
{
string message = ex.ToString();
x1++;
Thread.Sleep(1000);
}
}

}

This sounds like a bug to me. You should be able to create the same file
over and over again without having to wait on another process when in fact it
is referring to the process I am in. Are there any other suggestions out
there. I can't believe I am the only one with this problem.
Dave

Peter Duniho said:
[...]
Later in the program I try to create the file again. 50% of the time I
get
an error message saying the the file is currently being used by another
process. Can anyone tell me why I get this error and how I can get
around
this error?

It's impossible to know for sure. But, Windows does a variety of things
in the background with files that are created, such as indexing them,
trying to build a thumbnail, etc. This is usually what's going on when
you can't delete a file you just created.

The only solution I know of is to simply keep trying to delete it. Put
the delete in a loop that doesn't exit until you've successfully deleted
the file. For best results, make sure you call Thread.Sleep() between
attempts, with at least a 1 ms timeout.

Pete
 
P

Parrot

I don't understand why you don't think I showed how the file is being
created. My first post showed the following code:
FileStream fs1 = new FileStream(filename, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sr = new StreamWriter(fs1);

Between this code I perform numerous sr.WriteLine(record); instructions. At
the end I include the following closing instructions.

sr.Close();
sr.Dispose();
fs1.Close();
fs1.Dispose();

This is the only code that references that file. I come back to this
routine numerous times. I don't know what else I can show short of including
the 2000 line program. I admit I am not the world's greatest programmer
although I have 40 years experience writing COBOL and C. I guess I will just
have to keep scratching my head until I can stumble across something.


Peter Duniho said:
[...]
This sounds like a bug to me. You should be able to create the same file
over and over again without having to wait on another process when in
fact it
is referring to the process I am in.

I don't understand what you mean. Which process has the file open is
irrelevant. Even if it's the same process that's trying to delete it, you
can't delete it if there's a process with the file open.

The code you posted wasn't a concise-but-complete code sample. In
particular, it doesn't show how you're creating the file or using it.
Are there any other suggestions out
there. I can't believe I am the only one with this problem.

Well, I've written plenty of code that deletes files after it creates it.
Normally it works fine. If you are finding the file can't be deleted even
after trying repeatedly over 10 seconds, then you've got something hanging
on to the file more strongly than just some OS housekeeping.

But without a concise-but-complete code sample that reliably reproduces
the problem, it's not possible to say what might be happening.

Pete
 
H

Hans Kesting

Parrot pretended :
I am creating a file in my WIndows based C# program with the following
command: FileStream fs1 = new FileStream(filename, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sr = new StreamWriter(fs1);

I then write records to the file and then close both the FileStream and the
Streamwriter with the following commands:

sr.Close();
sr.Dispose();
fs1.Close();
fs1.Dispose();

Later in the program I try to create the file again. 50% of the time I get
an error message saying the the file is currently being used by another
process. Can anyone tell me why I get this error and how I can get around
this error?

Dave

Some guesses:
* are you sure the Close and Dispose are getting called? Could an
exception occur in writing the file, that causes the Close to be
skipped? (If so, see the 'using' statement)
* are you *reading* that file anywhere? That could cause the lock.

Hans Kesting
 
D

Dave

After spending hours tracing my program I finally figured out that I didn't
close the stream for another file that I accessed. This other file logic was
enclosed within a 2 deep function within the logic where I was having a
problem accessing the file in question. So by closing the other file in the
other logic section, I was able to access the file that I was being locked
out from. What is confusing is that the file that I was being locked out
from was a different stream from the file I had forgot to close. In summary,
it appears that if you leave any file open you may a problem opening up any
other file in your program. That to me is perplexing but nevertheless I
solved the problem through old fashioned hard work. Some problems are just
too complex to display program code on a forum.
Dave
 
D

Dave

After spending hours tracing my program I finally figured out that I didn't
close the stream for another file that I accessed. This other file logic was
enclosed within a 2 deep function within the logic where I was having a
problem accessing the file in question. So by closing the other file in the
other logic section, I was able to access the file that I was being locked
out from. What is confusing is that the file that I was being locked out
from was a different stream from the file I had forgot to close. In summary,
it appears that if you leave any file open you may a problem opening up any
other file in your program. That to me is perplexing but nevertheless I
solved the problem through old fashioned hard work. Some problems are just
too complex to display program code on a forum.
Dave
 
P

Parrot

To put it bluntly I don't have time to reproduce the code because it involves
bringing together segments of code from all parts of the program. I take the
approach if it works don't fix it. All I can tell you is that I closed
another file that was being processed after the file I was having problems
was being processed. After that I no longer have the problem. What else am
I to conclude? I have a program to finish. I appreciate the help I get from
these forums and if I have to endure arrogant slanders against my abilities
in the process, so be it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top