如何:检查不可用网络共享时防止超时 - C# [英] How To: Prevent Timeout When Inspecting Unavailable Network Share - C#

查看:139
本文介绍了如何:检查不可用网络共享时防止超时 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些基本的C#逻辑遍历目录并返回其中的文件夹和文件。当针对一个网络共享(\\server\share\folder),其是不可访问或无效运行,则代码似乎从调用返回回之前挂起为约30秒。



我想结束一个方法,试图从给定的路径获取文件夹和文件,但没有超时期间。换句话说,减少或完全消除超时。

我尝试了一些简单的事情,比如验证目录是否存在,网络驱动器会很快返回false,但这并没有按预期的那样工作。

  System.IO.Directory.Exists(path)/ /挂起

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path); //挂起

有什么建议可以帮助我实现一个高效的(希望管理的)解决方案? / p>

解决方案

把它放在自己的线程上,如果在一定的时间内没有回来,继续前进。

We have some basic C# logic that iterates over a directory and returns the folders and files within. When run against a network share (\\server\share\folder) that is inaccessible or invalid, the code seems to 'hang' for about 30 seconds before returning back from the call.

I'd like to end up with a method that will attempt to get folders and files from the given path, but without the timeout period. In other words, to reduce or eliminate the timeout altogether.

I've tried something as simple as validating the existence of the directory ahead of time thinking that an 'unavailable' network drive would quickly return false, but that did not work as expected.

System.IO.Directory.Exists(path) //hangs 

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path); //hangs

Any suggestions on what may help me achieve an efficient (and hopefully managed) solution?

解决方案

Place it on its own thread, if it doesn't come back in a certain amount of time, move on.

这篇关于如何:检查不可用网络共享时防止超时 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆