Pregunta

He creado un servicio de ventanas que se pueden mover archivos entre el disco duro del servidor (donde está instalado el servicio) a la unidad de red asignada en el servidor. uno de los problemas que tuve durante la creación de una solución se problemas de red.

¿Cómo puedo comprobar si existe una unidad de red al establecer un tiempo de espera en la comprobación de que? Si el tiempo de espera, cojo la excepción y reintento en X número de minutos y artículos de licencia en la cola.

Gracias!

¿Fue útil?

Solución

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/

EDIT

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);
   } 

Otros consejos

As far as I recall, using the normal System.IO methods do the trick. So in this case, you'd simply use:

if (Directory.Exists("Z:\\MyNetworkFolder"))
{
    // Gogogo
}
else
{
    Thread.Sleep(MyTimeout);
}

If the folder does not exist, pause for whatever timeout you choose. This should do the job perfectly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top