Syaiful Nizam Yahya
Syaiful Nizam Yahya

Reputation: 4305

WebClient close application when download completes

I'm downloading a huge file (700+MB) using WebClient. When the download completes, the application just closes itself. I tried debugging, but cannot capture anything.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var wc = new WebClient();
        wc.DownloadDataAsync(new Uri(@"http://192.168.1.100/FileServer/crypto.bin"));

    }
}

Is this a known bug?

Upvotes: 1

Views: 1731

Answers (2)

Jaime Oro
Jaime Oro

Reputation: 10135

I think the problem is that you must declare de wc variable outside the initializer.

    WebClient wc;

    public MainWindow()
    {
        InitializeComponent();

        wc = new WebClient();
        wc.DownloadDataAsync(new Uri(@"http://192.168.1.100/FileServer/crypto.bin"));

    }

Upvotes: 1

Jaime Oro
Jaime Oro

Reputation: 10135

You have to add an event, something like for example:

Private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}

Upvotes: 2

Related Questions