Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:30:03+00:00 2026-05-20T09:30:03+00:00

VS C# 2005 I am using the code below to upload a file Async

  • 0

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)
        {
            Console.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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:30:04+00:00Added an answer on May 20, 2026 at 9:30 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the code below, I am returning an nvarchar field from MS SQL 2005
In MS VisualStudio (I'm using 2005), it's possible to right-click in some code file
I am using VB.net code and SQL server 2005. I am havng below code
I'm using Visual Studio 2005 to script out a database so that I can
We are using SQL 2005 and the try-catch functionality to handle all of our
In the code below I am using a recursive CTE(Common Table Expression) in SQL
I am using the below code to find all the items in a listbox
I am using the below code protected void lnk_Click(object sender, EventArgs e) { try
I've got the following T-SQL code below that's working from inside of SQL Server
With SQL Server 2005 using SSMS, when I run this: SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.