我有一个 try/catch 方法,如下所示。当互联网连接断开时,try 中的代码阻塞,不进入 catch,应用程序停止响应。当互联网连接恢复时,问题就消失了,一切正常。
这是我的代码的一部分:
using (var fileStream = System.IO.File.OpenRead(strLocatie))
{
try
{
blobSAS.UploadFromStream(fileStream);
}
catch
{
}
}
我可以设置一个超时,以便当 try 块花费超过 1000 毫秒时,代码会自动进入 catch 吗?
整体方法:
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
{
}
}
如果互联网连接已经断开,在启动应用程序之前,catch 工作完美......