The program freezes for 30 seconds when accessing a disconnected drive

I have this piece of code that drives me crazy.

FUNCTION DiskInDrive(CONST DriveNumber: Byte): BOOLEAN;
VAR ErrorMode  : Word;
BEGIN
  RESULT:= FALSE;
  ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  TRY
    if DiskSize(DriveNumber) <> -1  { %%%% THIS IS VERY SLOW IF THE DISK IS NOT IN DRIVE !!!!!! }
    THEN RESULT:= TRUE;
  FINALLY
    SetErrorMode(ErrorMode);
  END;
END;

It checks if the drive is ready for use (and also if the specified drive letter matches the valid drive). The problem is that when I try to access a disconnected network drive (the network folder displayed as a drive), it freezes for about 10-30 seconds.

The code is in the constructor of component I.

How can I check the disk without waiting for this for a long time?

+3
source share
3 answers

, , - , Windows . , .

, . (Delphi 2009 generics Jon Schemitz 'MsgWaits .)

+3

-, , . , , - , (, , , , , ..).

, , , . Delphi ,

  • TThreadedObject DiskInDrive Execute
  • /
  • / .

, , . ( , , .)

+5

Create a component in delphi with a workflow and replace the call to this function with a procedure call that creates or takes a workflow from the pool and starts this work. then if the code expires, return FALSE. Therefore, the drive is not located on the drive if it is disconnected.

The code will close in due time and return the thread to the pool.

+1
source

Source: https://habr.com/ru/post/1728969/


All Articles