开发者

Catching execeptions using Async programming

开发者 https://www.devze.com 2023-02-14 19:16 出处:网络
VS C# 2005 I am using the code below to upload a file Async using the web client. However, I am not sure that I can catch the exception if there is a problem. Because the wc.UpLoadFileAsync would ret

VS C# 2005

I am using the code below to upload a file Async using the web client. However, I am not sure that I can catch the exception if there is a problem. Because the wc.UpLoadFileAsync would return immediately. So it seems pointless to put the try...catch in there.

So to test, I deliberately created an error to see what would happen if I put in an incorrect URL. However, I get this in my debug output window.

A first chance exception of type 'System.Net.WebException' occurred in System.dll
File completed

So it doesn't fire in the try...catch. And it still fires the UploadFileCompleted method

private void upload_config_to_server()
{
    Uri url = new Uri("http://10.10.10.3/softphone/config.xml");

    WebClient wc = new WebClient();
    if (!wc.IsBusy)
    {
        wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
        wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);      

        try
        {
            wc.UploadFileAsync(url, "PUT", "config.xml");
        }
        catch (WebException webex)
        {
            Consol开发者_StackOverflow社区e.WriteLine("Web Exception {0}", webex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception {0}", ex.Message);
        }
    }
}

private void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine("Bytes uploaded {0}", e.BytesSent);
}

private void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
    Console.WriteLine("File completed");
}  

Is there a correct way to handle this. I just want to show a message box to the user to display the reason why the upload file failed. However, as the uploaded file can be big it could take several seconds to upload it. So I need to have the Async to prevent the UI freezing up.

Many thanks for any suggestion,


You can check the e.Error value.

private void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
    if (e.Error != null)
    {
        if (e.Error is WebException)
            Console.WriteLine("Web Exception {0}", ((WebException)e.Error).Message);
        else
            Console.WriteLine("Web Exception {0}", e.Error.Message);
    }
    else if (e.Cancelled)
        Console.WriteLine("File cancelled");
    else
        Console.WriteLine("File completed");
} 


Since the call is asynchronous, your thread will continue executing and exit the try..catch block, so the exceptions will not occur there. Instead, you should check the Error property on the eventargs object in the UploadFileCompleted event handler. If there is an exception in the process, the Error property will contain the exception object. Also, I think it's a good idea to detach the event handlers there:

static void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
    wc.UploadProgressChanged -= wc_UploadProgressChanged;
    wc.UploadFileCompleted -= wc_UploadFileCompleted; 

    if (e.Error != null)
    {
        // there was an error
    }
}

This is a common approach in asynchronous programming; the asynchronous method catches the exception and stores it somewhere where you can examine it after the asynchronous call has completed.

0

精彩评论

暂无评论...
验证码 换一张
取 消