【发布时间】:2013-02-05 11:06:10
【问题描述】:
我有一个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 就完美了……
【问题讨论】:
-
它的 blobSAS 类型是什么?
-
blobSAS 是一个 CloubBlockBlob
-
您可以在尝试上传之前测试互联网连接,如果没有连接则跳过该部分代码。类似(这是伪代码) if(!InternetConnected) { Upload code } else { Catch code }
-
@Kobunite 如果在通话中互联网连接中断怎么办?
-
然后应用程序停止响应
标签: c# winforms azure try-catch