检查网络驱动器超时在C#中存在 [英] check if network drive exists with timeout in c#

查看:287
本文介绍了检查网络驱动器超时在C#中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的服务器的硬盘驱动器(安装服务)之间移动文件到服务器映射的网络驱动器的Windows服务。 ,同时创建一个解决方案是网络问题,我遇到的问题之一。

i created a windows service that move files around between the server's hard drive (where the service is installed) to the network drive mapped in the server. one of the problems I encountered while creating a solution was network problems.

我如何检查,而在检查它设置超时网络驱动器的存在?如果超时,我捕获异常和重试分钟的X次,并留在队列中的项目。

how do i check if a network drive exists while setting a timeout in checking it? If it times out, I catch the exception and retry in X number of minutes and leave items in queue.

谢谢!

推荐答案

将呼叫在一个单独的线程,并在一定超时后关闭线程。下面的链接实现了一个方法超时逻辑:

Put the call in a seperate thread and close the thread after a certain timeout. The following link implements a timeout logic for a method:

http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/

修改

一对话题的评论上述建议一更好的实现使用.NET异步模式:

One of the comments on the topic above suggest a better implementation using .NET Async Pattern:

public static T SafeLimex<T>(Func<T> F, int Timeout, out bool Completed)   
   {
       var iar = F.BeginInvoke(null, new object());
       if (iar.AsyncWaitHandle.WaitOne(Timeout))
       {
           Completed = true;
           return F.EndInvoke(iar);
       }
         F.EndInvoke(iar);
         Completed = false;
       return default(T);
   } 

这篇关于检查网络驱动器超时在C#中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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