Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a time-out for File.Create method on network drive?

I have a requirement in my application where I need to create the file in a network drive, but when file creation in progess if network disconnected application hangs for a while and throws an exception.

Is there any way we can set time-out for File.Create?

like image 413
Prashant Cholachagudda Avatar asked Nov 06 '22 00:11

Prashant Cholachagudda


1 Answers

Something like that can be used to implement time-out manually:

var fileCreatingThread = new Thread(...);
fileCreatingThread.Start();
if (fileCreatingThread.Join(TimeSpan.FromSeconds(5)) { /* Worked correctly */}
else
{
    // time-out
}
like image 180
Snowbear Avatar answered Nov 09 '22 12:11

Snowbear