开发者

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"));

  开发者_Python百科  }
}

Is this a known bug?


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!");
}


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"));

    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜