2016-05-04 16 views
0

このコードがうまくいかず、混乱しています。ファイルを正常にダウンロードしましたが、進捗状況をProgressBarに報告しません。私はすでにTimer1Timer1.Start()の前に使って、BackgroundWorker2.RunWorkerAsync()の前に始めました。プログレスバーでダウンロードの進行状況を表示する方法は? Visual Basic

Dim size As Double 
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork 
    Try 
     Dim G As Integer = 150 
     Dim Increase As Boolean = True 
     Do Until Clicked = True 
      If Increase = True Then 
       If Not G = 255 Then 
        G += 1 
        Threading.Thread.Sleep(10) 
       Else 
        Increase = False 
       End If 
      Else 
       If Not G = 150 Then 
        G -= 1 
        Threading.Thread.Sleep(10) 
       Else 
        Increase = True 
       End If 
      End If 
      Label6.ForeColor = Color.FromArgb(0, G, 0) 
     Loop 
     Label6.Cursor = Cursors.Default 
     Label6.Text = "Initializing" 
     Label6.ForeColor = Color.Lime 
     MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
     'WebBrowser1.Navigate(DownlaodLink) 
     'BackgroundWorker1.RunWorkerAsync() 
     ProgressBar1.Visible = True 
     size = TotalSize.Replace(" MBytes", "") 
     Me.Refresh() 
     Dim wc As New WebClient 
     wc.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip") 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

そして、私はこの作業を行う方法を見つけ出すことはできません私に私のダウンロード

Dim cursize As Double 
Dim finsize As Double 
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    If System.IO.File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip") Then 
     cursize = My.Computer.FileSystem.GetFileInfo(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip").Length 
     finsize = cursize/size * 100 

     If Not ProgressBar1.Value = ProgressBar1.Maximum Then 
      ProgressBar1.Value = finsize 
      ProgressBar1.Refresh() 
     Else 
      ProgressBar1.Value = finsize 
      ProgressBar1.Refresh() 
      Timer1.Stop() 
      MsgBox("Finished Downloading") 
     End If 
    End If 
End Sub 

の進行状況を表示するためのコード。誰か助けてくれますか?

+0

MSDNから:* DoWorkイベントハンドラ内のユーザーインターフェイスオブジェクトを操作しないように注意する必要があります。代わりに、ProgressChanged [...]イベントを介してユーザーインターフェイスに通信します。* Option Strictを有効にして[この例]を検討します(https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker .aspx) – Plutonix

+0

Opps、それについて忘れてしまった!私に笑を思い出させてくれてありがとう! –

+0

成功した場合は、自分の答えを受け入れることを忘れないでください。これは将来の読者が働く答えをより速く識別するのに役立ちます。また、この質問が解決され、問題を解決するために何らかの努力をする必要はなく、回答されていない質問に集中することがコミュニティに示しています。 –

答えて

0

最後に!私はそれを動作させたが、BackgroundWorkerと一緒に行かなかった。以下のコードは、私がこのことを機能させるために使用したものです。そしてそれはとても効率的で使いやすいです。

Public WithEvents downloader As WebClient 

Public Sub DownloadStart() 
    Label6.Cursor = Cursors.Default 
    Label6.Text = "Initializing" 
    Label6.ForeColor = Color.Lime 
    MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    ProgressBar1.Visible = True 
    downloader = New WebClient 
    downloader.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip") 
End Sub 

Private Sub downloader_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles downloader.DownloadProgressChanged 
    ProgressBar1.Value = e.ProgressPercentage 
End Sub 

私に手伝ってくれてありがとう!

関連する問題