⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.vb

📁 编程环境VB.NET2005 多线程下载
💻 VB
字号:
Public Class Form2
    Public threadw() As Boolean '//每个线程结束标志
    Public filenamew() As String '每个线程接收文件的文件名
    Public filestartw() As Integer '每个线程接收文件的起始位置
    Public filesizew() As Integer '每个线程接收文件的大小
    Public strurl As String '接受文件的URL
    Public hb As Boolean '文件合并标志
    Public thread As Integer '进程数
    Sub hbfile()
        While True
            hb = True
            For i As Integer = 0 To thread - 1
                If threadw(i) = False Then
                    '有未结束线程,等待
                    hb = False
                    System.Threading.Thread.Sleep(1000)
                    Exit Sub
                End If
            Next
            If hb = True Then
                Exit While
            End If
        End While

        '开始合并
        Dim fs, fstemp As IO.FileStream
        Dim readfile As Integer
        Dim bytes(512) As Byte
        fs = New IO.FileStream(textBox2.Text.Trim, IO.FileMode.Create)
        For k As Integer = 0 To thread - 1
            fstemp = New IO.FileStream(Me.filenamew(k), IO.FileMode.Open)
            While True
                readfile = fstemp.Read(bytes, 0, 512)
                If readfile > 0 Then
                    fs.Write(bytes, 0, readfile)
                Else
                    Exit For
                End If
            End While
            fstemp.Close()
        Next
        fs.Close()
        Dim dt As DateTime = DateTime.Now
        'textBox1.Text = dt.ToString '//结束时间 
        MessageBox.Show("接收完毕!!!")
    End Sub
    Sub StartDown()
        Dim dt As DateTime = DateTime.Now
        textBox4.Text = dt.ToString
        strurl = textBox1.Text
        Dim filesize As Long
        Dim request As Net.HttpWebRequest
        Try
            request = CType(Net.HttpWebRequest.Create(strurl), Net.HttpWebRequest)
            filesize = request.GetResponse.ContentLength
            request.Abort()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        thread = CInt(textBox3.Text) '接收线程数
        '根据线程数初始化数组
        threadw = New Boolean(thread) {}
        filenamew = New String(thread) {}
        filestartw = New Integer(thread) {}
        filesizew = New Integer(thread) {}

        '//计算每个线程应该接收文件的大小
        Dim filethread As Integer = CInt(CInt(filesize) / thread) '平均分配
        Dim filethreade As Integer = filethread + CInt(filesize) Mod thread '剩余部分由最后一个线程完成

        '//为数组赋值
        For i As Integer = 0 To thread - 1
            threadw(i) = False '每个线程状态的初始值为假
            filenamew(i) = i.ToString & ".dat" '每个线程接收文件的临时文件名
            If i < thread - 1 Then
                filestartw(i) = filethread * i
                filesizew(i) = filethread - 1
            Else
                filestartw(i) = filethread * i '每个线程接收文件的起始点
                filesizew(i) = filethread - 1 '每个线程接收文件的长度
            End If
        Next
        '  //定义线程数组,启动接收线程
        Dim threadk(thread) As Threading.Thread
        Dim httpfile(thread) As HttpFile

        For j As Integer = 0 To thread - 1
            httpfile(j) = New HttpFile(Me, j)
            threadk(j) = New Threading.Thread(AddressOf httpfile(j).receive)
            threadk(j).Start()
        Next
        '//启动合并各线程接收的文件线程
        Dim hbth As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf Me.hbfile))
        hbth.Start()

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        Me.StartDown()
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -