微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb.net winforms下载不受阻碍

如何解决vb.net winforms下载不受阻碍

我正在下载.mp3文件,我的目标是在下载过程中甚至不冻结最低GUI。 我的目的还在于在进度栏中和通过标签显示收到的叮咬。 该代码有效,但有时无故冻结,有时进度条在文件完全完成后才起作用。 到目前为止,这是我在网上找到的“最佳”代码,用于在下载过程中完全正常运行的进度条,但仍然遇到问题。 您如何看待我可以提高表现?如何制作耐久和可靠的工作进度栏?如何在不冻结GUI的情况下下载大文件?我试着(好奇心)下载一个600 MB的文件,它完全死机,没有响应,也没有给任何问题。 谢谢

编辑1:尽管我迷茫不已,但我正在尝试使用此方法。关于如何使用此代码并将其插入 Jimi答案的任何想法? Answer


Imports System.IO
Imports System.IO.Path
Imports System.Net



Public Class Form1
  Private downloader As MyDownloader = nothing

  Private Sub btnStartDownload_Click(sender As Object,e As EventArgs) Handles btnStartDownload.Click
      Dim progress = New Progress(Of String)(
          Sub(data)

              MsgBox("we are on the UI thread here")
          End Sub)

      Dim url As Uri = New Uri(TextBox1.Text)
      downloader = New MyDownloader()
      'How can I remove this second? I don't need download from url every 1 second. 
      downloader.StartDownload(progress,url,1)

  End Sub

Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Net
Imports System.Net.Http
Imports System.Text.RegularExpressions
Imports System.Threading

Public Class MyDownloader
    Private Shared ReadOnly client As New HttpClient()
            client.DownloadProgressChanged += AddressOf Client_DownloadProgressChanged
            client.DownloadFileCompleted += AddressOf Client_DownloadFileCompleted
    Private interval As Integer = 0
    Private Sub Client_DownloadFileCompleted(ByVal sender As Object,ByVal e As AsyncCompletedEventArgs)
        System.Windows.Forms.MessageBox.Show("Download OK!","Message",MessageBoxButtons.OK,MessageBoxIcon.information)
    End Sub
    Public Sub StartDownload(progress As IProgress(Of String),url As Uri,intervalSeconds As Integer)
        interval = intervalSeconds * 1000
        Task.Run(Function() DownloadAsync(progress,url))
    End Sub
    Private Sub Client_DownloadProgressChanged(ByVal sender As Object,ByVal e As DownloadProgressChangedEventArgs)
        ProgressBar1.Minimum = 0
        Dim receive As Double = Double.Parse(e.BytesReceived.ToString())
        Dim total As Double = Double.Parse(e.TotalBytesToReceive.ToString())
        Dim percentage As Double = receive / total * 100
        label2.Text = $"{String.Format("{0:0.##}",percentage)}%"
        ProgressBar1.Value = Integer.Parse(Math.Truncate(percentage).ToString())
    End Sub
    Private Async Function DownloadAsync(progress As IProgress(Of String),url As Uri) As Task
        Dim pattern As String = "<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>"
        Dim downloadTimeWatch As Stopwatch = New Stopwatch()
        downloadTimeWatch.Start()
        Do
            Try
                Dim response = Await client.GetAsync(url,HttpCompletionoption.ResponseContentRead)
                Dim data = Await response.Content.ReadAsstringAsync()

                data = WebUtility.HtmlDecode(Regex.Replace(data,pattern,""))
                progress.Report(data)

                Dim delay = interval - CInt(downloadTimeWatch.ElapsedMilliseconds)
                Await Task.Delay(If(delay <= 0,10,delay))
                downloadTimeWatch.Restart()

            Catch ex As Exception
            End Try
        Loop
    End Function
End Class

我迷上了它,我试图删除取消下载,因为我不会停止任何下载,我还尝试了每1秒从url删除一次下载,因为每个链接只需要一次下载。 谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。