Delphi HTTP App raising error when loading files into streams

653 views Asked by At

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?

1

There are 1 answers

2
Darian Miller On BEST ANSWER

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);