Index: JclDebug.pas
===================================================================
--- JclDebug.pas	(revision 31867)
+++ JclDebug.pas	(revision 31868)
@@ -2250,6 +2250,36 @@
   VirtualAlignedSize: DWORD;
   I, X, NeedFill: Integer;
 
+  function TryOpenExeStream(const _ExeFileName: string): TFileStream;
+  const
+    MaxTries = 10;
+  var
+    Tries: Integer;
+  begin
+    Tries := 1;
+    while True do begin
+      try
+        Result := TFileStream.Create(_ExeFileName, fmOpenReadWrite or fmShareExclusive);
+        // if we get here, it worked, so we simply exit
+//        WriteLn('Opening executable worked on ', Tries, ' try.');
+        Exit; //==>
+      except
+        on EFOpenError do begin
+          // If opening the file failed, it's probably the virus scanner which is currently
+          // accessing it. So we wait a second and try again.
+          if Tries >= MaxTries then begin
+            // we already failed too many times, give up
+            raise;
+          end;
+          Inc(Tries);
+//          WriteLn('Opening executable failed, will try again in 1 second.');
+          // sleep 1 second and try again
+          Sleep(1000);
+        end;
+      end;
+    end;
+  end;
+
   procedure RoundUpToAlignment(var Value: DWORD; Alignment: DWORD);
   begin
     if (Value mod Alignment) <> 0 then
@@ -2275,7 +2305,7 @@
   if not Result then
     Exit;
 
-  ImageStream := TFileStream.Create(ExecutableFileName, fmOpenReadWrite or fmShareExclusive);
+  ImageStream := TryOpenExeStream(ExecutableFileName);
   try
     try
       if PeMapImgTarget(ImageStream, 0) = taWin32 then
