admin管理员组

文章数量:1278789

In my Windows computer, I have some network drives mapped. At a given time, I want to check which drive is available and which is not. Every method I used (WNetGetConnection, GetDiskFreeSpaceEx, GetVolumeInformation, ...) blocks my program for about a minute if that drive is not available (server not online). I guess it has a timeout waiting for the drive to connect.

Is there a way to check this without blocking my program? I don't want to use another thread just for that.

In my Windows computer, I have some network drives mapped. At a given time, I want to check which drive is available and which is not. Every method I used (WNetGetConnection, GetDiskFreeSpaceEx, GetVolumeInformation, ...) blocks my program for about a minute if that drive is not available (server not online). I guess it has a timeout waiting for the drive to connect.

Is there a way to check this without blocking my program? I don't want to use another thread just for that.

Share Improve this question edited Feb 25 at 7:41 Toby Speight 30.9k50 gold badges75 silver badges113 bronze badges asked Feb 24 at 20:13 Marus GradinaruMarus Gradinaru 3,1203 gold badges27 silver badges65 bronze badges 9
  • 7 "I don't want to use another thread just for that" - Why not? It could monitor the drives periodically and save that result somewhere, and then you simply check your saved value when needed. – Remy Lebeau Commented Feb 24 at 20:25
  • 2 Remy is right, I want to add the fact that even if you "check" that the drive is OK at any given time, the next real access is not guaranteed to be OK. Just read the file when you need it and let Windows do the rest. If you are afraid of blocking (which you shouldn't since you have mapped and network drives are expected to lag anyway), use a thread. – Michael Chourdakis Commented Feb 24 at 21:45
  • 5 A more legitimate way to ask this - how does one perform a network file read with an explicit timeout? It's not the lag per se that annoys and makes the app look frozen, it's that the lag on the order of a minute. – Seva Alekseyev Commented Feb 24 at 21:55
  • 1 Depending on the network details, you may first do a ping to know if the remote computer is alive (server online). Ping respond quickly. If ping respond within a short time, you then try to access the file. You have to determine if you are in a case where ping is working. To do the ping, you may use ICS or Indy or even Windows API. – fpiette Commented Feb 25 at 7:45
  • 2 @fpiette unless of course answers to ICMP requests have been disabled on the server. That's the default for Windows clients since Windows XP, no idea if it is the same for Windows server. If these answers are disabled, it will seem as if the server is not available. – dummzeuch Commented Feb 25 at 8:57
 |  Show 4 more comments

1 Answer 1

Reset to default 5

Because the Windows API that is called to retrieve the folder has its own delay (which cannot be changed), you have no other option but to use a thread and handle the event yourself.

I achieve this by attempting to access the directory in a separate thread. If the timeout is reached, I exit the function. This does not completely free the main thread from waiting, but it makes the waiting time more manageable until you receive a response.

A function could look like this:

function NetworkPathAvailableWithTimeOut(const _NetworkPath: string; _TimeOut: Cardinal): Boolean;
var
  l_PathAvailable: Boolean;
  l_EndTime: UInt64;
  l_TaskDone: Boolean;
begin
  l_PathAvailable := False;
  l_TaskDone := False;
  l_EndTime := GetTickCount64 + _TimeOut;

  // Run the check in a separate thread to avoid blocking
  TThread.CreateAnonymousThread(
    procedure
    begin
      l_PathAvailable := System.SysUtils.DirectoryExists(_NetworkPath);
      l_TaskDone := True;
    end
  ).Start;

  // Wait for the task to complete or timeout
  while not l_TaskDone do
  begin
    if GetTickCount64 >= l_EndTime then
      Exit(False); // Timeout reached, assume unavailable
    Sleep(50); // Prevent CPU overuse
  end;

  Result := l_PathAvailable;
end;

本文标签: windowsHow to check if a network drive is availableinstantlyStack Overflow