BackgroundWorker不想停止工作

时间:2016-01-10 17:04:32

标签: .net vb.net visual-studio-2015 backgroundworker

我创建了一个用于下载文件的简单程序,下载第一组然后开始下载另一个程序并从列表中下载文件。 但有时停在3档并下载。 我调查过以前的下载即使文件下载到100%也没有完成。

Private Sub Download(ByVal Down As Integer)
    On Error Resume Next
    Dim req As System.Net.WebRequest
    Dim resp As System.Net.WebResponse
    TextBox1.Text = Convert.ToString("http://example.com/files/" + Convert.ToString(Down) + ".zip")
    TextBox2.Text = Convert.ToString(Down)
    req = Net.WebRequest.Create(TextBox1.Text)
    resp = req.GetResponse
    req.Method = Net.WebRequestMethods.Http.Get
    download_size = resp.ContentLength
    ProgressBar1.Maximum = download_size
    Console.WriteLine(BackgroundWorker1.IsBusy)
    BackgroundWorker1.RunWorkerAsync()
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    On Error Resume Next
    downloaded_size = My.Computer.FileSystem.GetFileInfo(FlatTextBox1.Text + "\" + TextBox2.Text + ".zip").Length
    ProgressBar1.Value = downloaded_size
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        Timer1.Stop()
        BackgroundWorker1.CancelAsync()
        If Timer2.Enabled = False Then
            Timer2.Start()'Timer 5sec to to start function Download(x) and ProgressBar set to 0
        End If
    End If
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    On Error Resume Next
    Console.WriteLine(TextBox1.Text + " | " + FlatTextBox1.Text + "\" + TextBox2.Text + ".zip")
    My.Computer.Network.DownloadFile(TextBox1.Text, FlatTextBox1.Text + "\" + TextBox2.Text + ".zip", "", "", False, 360000, True)

End Sub

1 个答案:

答案 0 :(得分:1)

下载文件比使用BackgroundWorker更好的方法是 WebClient ,功能 DownloadFileAsync 。 但是必须在工具箱中启用WebClient。

$('#open').click(function(e) {
  e.preventDefault();
  e.stopImmediatePropagation();
  $("#dialog").dialog('open');
});