at general, you should raise a exception if the task should still if it alread started... else, you can do the workaround and continue.
try some like this: button1 = try open the file -- using fmOpenShareXXX/ZZZ button2 = try open the same file -- using fmOpenShareXXX/ZZZ ---- implementation
{$R *.dfm}
const arquivo: string = 'test.dcu';
var arq1: TFileStream = nil; arq2: TFileStream = nil;
function MyLoadFile(AFileName: string): TFileStream; begin result := nil; // if FileExists(arquivo) then // FileExists() already try take care about possibles errors ... "GetLastError..." try result := TFileStream.Create(arquivo, fmOpenRead or fmShareDenyRead { fmShareDenyWrite / fmShareExclusive } ); // simulating a "DenyRead" in all cases // if success... return the file opened! except // here show error just for your test! on E: EFOpenError do ShowMessage('First: the Exception for this error: ' + E.ClassName); on E: Exception do ShowMessage('for last: Generic Exception: ' + E.ClassName); end; end;
procedure TForm1.Button1Click(Sender: TObject); begin arq1 := MyLoadFile(arquivo); // if not(arq1 = nil) then ShowMessage('hello world to arq1'); end;
procedure TForm1.Button2Click(Sender: TObject); begin arq2 := MyLoadFile(arquivo); // if not(arq2 = nil) then ShowMessage('hello world to arq2'); end;
procedure TForm1.Button3Click(Sender: TObject); begin arq1.Free; arq1 := nil; end;
procedure TForm1.Button4Click(Sender: TObject); begin arq2.Free; arq2 := nil; end;
procedure TForm1.FormDestroy(Sender: TObject); begin arq1.Free; arq2.Free; end;
end.
----------
function MyLoadFile(AFileName: string): TFileStream; begin result := nil; // if FileExists(arquivo) then // FileExists() already try take care about possibles errors ... "GetLastError..." try result := TFileStream.Create(arquivo, fmOpenRead or fmShareDenyRead { fmShareDenyWrite / fmShareExclusive } ); // simulating a "DenyRead" in all cases // if success... return the file opened! except raise; end;
procedure TForm1.Button1Click(Sender: TObject); begin // if you need know the exception on FORM caller! try arq1 := MyLoadFile(arquivo); // except on E: Exception do ShowMessage(E.ClassName + sLineBreak + E.Message); end; // if not(arq1 = nil) then ShowMessage('hello world to arq1'); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
my flaw: in general, you should create an exception if the task has already started and you shouldn't continue after an error that would compromise the result. If not, you could find another way to complete it.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
function MyLoadFile(AFileName: string; out ALastError: integer): TFileStream; begin result := nil; // if FileExists(arquivo) then // FileExists() already try take care about possibles errors ... "GetLastError..." try result := TFileStream.Create(arquivo, fmOpenRead or fmShareDenyRead { fmShareDenyWrite / fmShareExclusive } ); // simulating a "DenyRead" in all cases // if success... return the file opened! except on E: Exception do begin ALastError := GetLastError; // raise; // the GetLastError value can changes after this... end; end; end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
No, if FILE-NOT-EXISTS should not raise any exception, at general! Exceptions, only in extreme cases!
GetLastError = ERROR_FILE_NOT_FOUND {2}
GetLastError = ERROR_SHARING_VIOLATION {32}
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3