System.IO.DriveInfo.GetDrives() 因网络驱动器断开连接而挂起
我正在.NET 3.5 中编写一个文件管理器。启动时,应用程序必须枚举可用的驱动器。我正在使用 DriveInfo.GetDrives 来做到这一点。
不幸的是,我的用户抱怨说,当他们断开网络驱动器时,应用程序需要大约 30 秒才能启动。
我发现当网络驱动器断开连接时,GetDrives() 函数会挂起很多秒。用户界面冻结。这种情况发生在启动时,以及每次我连接新驱动器时。
总而言之,我需要一种方法来枚举立即返回的可用驱动器,即使存在断开连接的网络驱动器。感谢您的任何帮助。
附加信息:
我用WMI做了一些测试,也出现了同样的问题。 WMI 查询在 15 秒左右返回。
我还考虑将对 DriveInfo 的调用移至单独的线程。这样就解决了不阻塞用户界面的问题;但这意味着,当用户连接新驱动器时,驱动器图标只会在 15 秒后出现。这并不理想。
非常感谢您的帮助。
I am writing a file manager in .NET 3.5. At startup, the application must enumerate available drives. I am using DriveInfo.GetDrives to do that.
Unfortunately my users lamented that, when they have disconnected network drives, the application took about 30 seconds to start.
I discovered that the GetDrives() function hangs for many seconds when you have a disconnected network drive. And the UI freezes. This happens at startup, and each time I attach a new drive.
To sum up, I need a way to enumerate available drives which returns immediately, even though there are disconnected network drives. Thanks for any help.
Additional informations:
I did some test with WMI, and the same problem arises. The WMI query returns after 15 seconds or so.
I have also considered moving the call to DriveInfo to a separate thread. This would solve the problem of not blocking the user interface; but it would mean that, when the user attaches a new drive, the drive icon would only appear after 15 seconds. That is not ideal.
Thank you very much for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我担心没有办法避免阻塞。这是由于 Windows API 中的网络/文件超时造成的。
I fear that there isno way to avoid the blocking. It is due to network/file timeouts in Windows API.