Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod to download large files in Vb.net
    primarykey
    data
    text
    <p>Okay, so I have been trying to download a large file with different methods. In my code what would usually work best is <code>My.Computer.NetWork.DownloadFile</code> but because the file is 1.5Gb's my windows form freezes and doesn't respond. I didn't bother waiting to see for how long it wouldn't respond for after I waited 5 minutes because I thought it would just be a waste of time. So I also tried <code>wc.DownloadFileAsync</code> (wc standing for Web Client) this works and doesn't freeze my windows form but the problem with this is that it skips over it and doesn't wait until the download is finished so it continues on with my code and therefore I get errors.</p> <p>I tried researching ways to pause or stop the code until the download was finished but no luck. After further research I found the backgroundworker class. I was wondering if this would work for me and how would I implement it into my code, or if there is any easier way to go about doing this?</p> <p>I was not able to successfully implement it into my code. I wasn't able to invoke and therefore got errors such as this: <code>Cross-thread operation not valid</code>.</p> <p>This is currently my code, with the background worker:</p> <pre><code> Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim downloader As New System.Net.WebClient Dim ServerVersion = wc.DownloadString("http://127.0.0.1:8080/patch/PatchList.txt").Trim Dim tLines As String() = ServerVersion.Split(Environment.NewLine) For Each NewLine As String In tLines Dim tVersionAndUrl As String() = NewLine.Split(vbTab) Dim encText As New System.Text.UTF8Encoding() Dim btText() As Byte btText = encText.GetBytes(tVersionAndUrl(0)) 'MessageBox.Show(btText.ToString) 'MessageBox.Show(tVersionAndUrl(0)(0)) If tVersionAndUrl.Length &lt; 2 Then Exit For End If If Integer.Parse(tVersionAndUrl(0)) &lt; Integer.Parse(CVersion.Text) Then Dim TempPath As String = "\launcher\temp.rar" AddHandler wc.DownloadProgressChanged, AddressOf ProgressChanged AddHandler wc.DownloadProgressChanged, AddressOf ProgressChanged AddHandler wc.DownloadFileCompleted, AddressOf DownloadCompleted 'wc.DownloadFileAsync(New Uri(tVersionAndUrl(1)), Me.GetFileName(tVersionAndUrl(1))) wc.DownloadFileAsync(New Uri(tVersionAndUrl(1)), tmp, Stopwatch.StartNew) 'My.Computer.FileSystem.DeleteFile(Me.GetFileName(tVersionAndUrl(1))) CVersion.Text = tVersionAndUrl(0) LabelStatus.Text = "Download in Progress" Button1.Enabled = False End If Next MsgBox("Client is up to date") End Sub </code></pre> <p>And this is the Addhandlers for it:</p> <pre><code> Private Sub ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString()) Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString()) Dim percentage As Double = bytesIn / totalBytes * 100 ProgressBarCurrent.Value = Int32.Parse(Math.Truncate(percentage).ToString()) Dim BytesDownloaded As String = (e.BytesReceived / (DirectCast(e.UserState, Stopwatch).ElapsedMilliseconds / 1000.0#)).ToString("#") If BytesDownloaded &lt; 1024 Then Dim Bs As String = Convert.ToInt32(BytesDownloaded) Label4.Text = (Bs &amp; " B/s") ElseIf BytesDownloaded &lt; 1048576 Then Dim KBs As String = Math.Round(BytesDownloaded / 1024, 2) Label4.Text = (KBs &amp; " KB/s") ElseIf BytesDownloaded &lt; 1073741824 Then Dim MBs As String = Math.Round(BytesDownloaded / 1048576, 2) Label4.Text = (MBs &amp; " MB/s") End If End Sub Private Sub DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) 'MessageBox.Show("Download Complete") LabelStatus.Text = "Download Complete" Button1.Enabled = True Downloading = False End Sub </code></pre> <p>I would appreciate any help. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload