I have a try/catch method, as shown below. When the internet connection is down, the code blocks in the try, and does not enter the catch, and the application stops responding. When the internet connection is back, the problem is gone, and everything works fine.
This a part of my code:
using (var fileStream = System.IO.File.OpenRead(strLocatie))
{
try
{
blobSAS.UploadFromStream(fileStream);
}
catch
{
}
}
Can I set a timeout such that when the try block takes longer than 1000ms the code automatically goes to the catch?
Whole method:
public void uploadImages(string strLocatie, string naamBestand, string directoryname)
{
try
{
string dag = DateTime.Now.Day.ToString();
if (dag.Length == 1)
{
string temp = dag;
dag = "0" + temp;
}
string maand = DateTime.Now.Month.ToString();
if (maand.Length == 1)
{
string temp = maand;
maand = "0" + temp;
}
if (signature == null)
{
getKey();
}
string datum = dag + "-" + maand + "-" + DateTime.Now.Year.ToString();
CloudBlobClient blobClient = new CloudBlobClient(sUrl, new StorageCredentialsSharedAccessSignature(signature));
CloudBlobContainer blobContainer = blobClient.GetContainerReference(container1);
blobContainer.GetDirectoryReference(sUrl + container1);
CloudBlockBlob blobSAS = new CloudBlockBlob(sUrl + container1 + "/" + directoryname + "/" + datum + "/" + naamBestand,
new StorageCredentialsSharedAccessSignature(signature));
using (var fileStream = System.IO.File.OpenRead(strLocatie))
{
try
{
blobSAS.UploadFromStream(fileStream);
}
catch
{
}
}
File.Delete(strLocatie);
}
catch
{
}
}
And if the internet connection is allready down, before starting the application, the catch works perfect…
You have to set the blobrequestoptions of your CloudBlockBlob. Here you can set a timeout. However it would be better to upload the file in a backgroundworker. This way your application is always responsive.
blobrequestoptions