4

I've been working on an HTTP Web Server Application (via TIdHTTPWebBrokerBridge) and have a little issue when it comes to loading files into streams (TFileStream) to be sent back to the client. It doesn't happen every time, but quite randomly... I keep getting an exception...

Cannot open file "C:\SomePath\SomeFile.html". The process cannot access the file because it is being used by another process

It happens on this line:

Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead);

(Str being a TFileStream)

I'm assuming the message speaks for its self, but I absolutely need to avoid it. This exception only happens in debug mode, but I need to debug this thing without worrying about getting this message all the time.

Strangely, most of the time, the file is loaded and sent back anyway.

How could I go about avoiding this? And why might it not allow me to open it more than once, even if its read only?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 4
    Well, you don't use the share mode in your code... perhaps using `fmShareDenyWrite` or `fmShareDenyNone` is all thats needed to fix your problem, see http://docwiki.embarcadero.com/Libraries/en/System.Classes.TFileStream.Create – ain Mar 24 '12 at 17:38

1 Answers1

6

As @ain stated in the comment - you are missing the share mode in your Constructor.

Change this

Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead);

to this

Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead or fmShareDenyNone);
Darian Miller
  • 7,808
  • 3
  • 43
  • 62