📄 mydownthread.vb
字号:
Imports System.Threading
Public Class MyDownThread
#Region "dim"
Private m_filename As String
Private m_startPos As Long
Private m_endPos As Long
Private m_nowPos As Long
Private m_url As String
Friend m_speed As Long
Private memDiskSize As Integer = 100000 * 100
Private bufferSize As Integer = CInt(Me.memDiskSize / 100)
Friend speedlimit As Boolean = True
Friend limitspeedtime As Integer
Private m_stop As Boolean = False
Friend Event thStop(ByVal sender As MyDownThread, ByVal saveData As MyThreadStru)
Private m_threadData As MyThreadStru
' Friend clearSpeedSum As Boolean '速度是否清空
Friend writeCount As Integer
Friend thisThNoErrStop As Boolean = False
Friend thErrCount As Integer = 0
#End Region
Public Property MyThreadData() As MyThreadStru
Get
m_threadData.startPos = Me.m_startPos
m_threadData.nowPos = Me.m_nowPos
m_threadData.endPos = Me.m_endPos
m_threadData.speed = Me.m_speed
Return m_threadData
End Get
Set(ByVal value As MyThreadStru)
m_threadData = value
Me.m_url = m_threadData.url
Me.m_filename = m_threadData.filename
Me.m_startPos = m_threadData.startPos
Me.m_nowPos = m_threadData.nowPos
Me.m_endPos = m_threadData.endPos
End Set
End Property
Public Sub New(ByVal nt As MyThreadStru)
Me.MyThreadData = nt
End Sub
Sub startdown()
'If Me.MyThreadData.nowPos = (Me.MyThreadData.endPos - Me.MyThreadData.startPos + 1) Then
' RaiseEvent thStop(Me, Me.MyThreadData)
' Exit Sub
'End If
Me.m_stop = False
System.Threading.Thread.Sleep(Me.thErrCount * 1000)
Me.DoDownload()
Exit Sub
Dim th As Threading.Thread
th = New Threading.Thread(AddressOf Me.DoDownload)
th.Start()
End Sub
Sub stopDown()
Me.m_stop = True
End Sub
Sub DoDownload()
If Me.m_nowPos = Me.m_endPos + 1 Then
Me.thisThNoErrStop = True
RaiseEvent thStop(Me, Me.MyThreadData)
Exit Sub
End If
'********************************************************
'http://dugupiaoyun.icode.csdn.net/post/2007/06/21/54243
Dim ns As System.IO.Stream = Nothing : Dim request As Net.HttpWebRequest
Dim fs As New IO.FileStream(Me.m_filename, System.IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.ReadWrite)
Dim b(bufferSize - 1) As Byte : Dim ret As Integer
Dim memB(memDiskSize - 1) As Byte
Try
request = CType(Net.HttpWebRequest.Create(Me.m_url), Net.HttpWebRequest)
'接收的起始位置及接收的长度
request.AddRange(CInt(Me.m_nowPos), CInt(Me.m_endPos))
ns = request.GetResponse.GetResponseStream
fs.Seek(Me.m_nowPos, IO.SeekOrigin.Begin)
Dim L As Integer = 0
Do
ret = ns.Read(b, 0, bufferSize)
If L + ret >= memDiskSize Then '要超过时
fs.Write(memB, 0, L)
writeCount += 1
L = ret
Else
L += ret
End If
Array.Copy(b, 0, memB, L - ret, ret)
Me.m_speed += ret
Me.m_nowPos += ret
If Me.m_stop = True Then Exit Do
If Me.speedlimit = True Then
'System.Threading.Thread.Sleep(limitspeedtime)
End If
'While speedlimit
' ' System.Threading.Thread.Sleep(100)
' If Me.m_stop = True Then Exit Do
'End While
'System.Threading.Thread.Sleep(200)
Loop Until ret = 0
fs.Write(memB, 0, L)
writeCount += 1
'ret = ns.Read(b, 0, bufferSize)
'While ret > 0
' If Me.m_stop = True Then Exit While
' fs.Write(b, 0, ret)
' Me.m_speed += ret
' Me.m_nowPos += ret
' While speedlimit
' System.Threading.Thread.Sleep(10)
' If Me.m_stop = True Then Exit While
' End While
' ret = ns.Read(b, 0, bufferSize)
'End While
Me.m_speed = 0
fs.Close()
ns.Close()
Me.thisThNoErrStop = True
Me.thErrCount = 0
RaiseEvent thStop(Me, Me.MyThreadData)
Catch ex As Exception
Me.thisThNoErrStop = False
Me.thErrCount += 1
RaiseEvent thStop(Me, Me.MyThreadData)
Debug.WriteLine("出错:" & Me.MyThreadData.startPos.ToString & " time:" & Now.ToString & " : " & ex.Message)
Finally
If fs Is Nothing = False Then fs.Close()
If ns Is Nothing = False Then ns.Close()
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -