2

所以我终于让它几乎可以工作了,但现在每隔几次我都会测试表单和进度条冻结的过程。我也确信有更有效的方法可以做到这一点,因此任何建设性的批评将不胜感激。

这是程序一页的编码,允许用户单击一个按钮下载并安装一个应用程序,然后按下一个按钮下载并安装另一个应用程序:

Imports System.Net.WebRequestMethods
Public Class Software

'Open link in external browser
Public Sub HandleRequestNavigate(ByVal sender As Object, ByVal e As RequestNavigateEventArgs)
    Process.Start(New ProcessStartInfo(e.Uri.AbsoluteUri))
    e.Handled = True
End Sub

'Declarations
Shared progressamc As New Progress
Shared progresscti As New ProgressCTI
WithEvents startcti As New Process
WithEvents startamc As New Process
WithEvents startsfstb As New Process
WithEvents amcworker As New ComponentModel.BackgroundWorker
WithEvents ctiworker As New ComponentModel.BackgroundWorker
Dim ProgressBarAMC As Object = Progress.ProgressBar1
Dim blprgrsAMC As Object = Progress.blprgrs
Dim ProgressBarCTI As Object = progresscti.ProgressBar1
Dim blprgrsCTI As Object = progresscti.blprgrs


'FTP Values
Const host As String = "ftp://10.167.16.80/"
Const username As String = "anonymous"
Const password As String = ""
'AMC File Put/Get
Const localfileamc As String = "C:\AMC.exe"
Const Remotefileamc As String = "Bin/AMC.exe"
'CTI File Put/Get
Const localfilecti As String = "C:\CTI.exe"
Const Remotefilecti As String = "Bin/CTI.exe"

'On Init
Public Sub New()
    InitializeComponent()
    amcworker.WorkerReportsProgress = True
    amcworker.WorkerSupportsCancellation = True
    ctiworker.WorkerReportsProgress = True
    ctiworker.WorkerSupportsCancellation = True
End Sub

'Install AMC Button
Private Sub ButtonAMC(sender As Object, e As RoutedEventArgs)
    Dim butt1 As Button = DirectCast(sender, Button)
    butt1.IsEnabled = False
    Dispatcher.BeginInvoke(New Action(AddressOf progressamc_Show))
    AddHandler Progress.Cancel_Click, AddressOf myProcessamc_Exited
    amcworker.RunWorkerAsync()
End Sub

'Open Dialog
Private Sub progressamc_Show()
    Try
        progressamc.ShowDialog()
    Catch ex As Exception
        MessageBox.Show("An error has occurred during the process:" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Please close the application and try again." _
                & vbCrLf & "If you continue to encounter this error please Email")
    End Try
End Sub

'FTP - Download
Private Sub ftpseshamc_DoWork(ByVal sender As System.Object, ByVal e As ComponentModel.DoWorkEventArgs) Handles amcworker.DoWork
    Dim URI As String = host & Remotefileamc
    Dim FTP As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
    'Set the credentials
    FTP.Credentials = New System.Net.NetworkCredential(username, password)
    'FTP Options
    FTP.KeepAlive = False
    FTP.UseBinary = True
    'Define the action as Download
    FTP.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
    'Get the response to the Ftp request and the associated stream
    Try
        Dim response As System.Net.FtpWebResponse = CType(FTP.GetResponse, System.Net.FtpWebResponse)
        Dim Length As Long = response.ContentLength
        Dim StopWatch As New Stopwatch
        Dim CurrentSpeed As Double = Nothing
        Using responseStream As IO.Stream = response.GetResponseStream
            'loop to read & write to file
            Using fs As New IO.FileStream(localfileamc, IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Dim count As Integer
                Do
                    If amcworker.CancellationPending = True Then
                        e.Cancel = True
                        Return
                    End If
                    StopWatch.Start()
                    amcworker.ReportProgress(CShort(count / Length * 100 + 0.5))
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                    count += read
                Loop Until read = 0
                StopWatch.Stop()
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        response.Close()
    Catch ex As Exception
        MessageBox.Show("An error has occurred during the process:" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Please close the application and try again." _
                        & vbCrLf & "If you continue to encounter this error please Email")
        myProcessamc_Exited()
    End Try
    Installamc()
End Sub

'Starts the installation
Sub Installamc()
    startamc.StartInfo.FileName = "C:\AMC.exe"
    startamc.EnableRaisingEvents = True
    Try
        startamc.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Dispatcher.Invoke(New Action(AddressOf Progressamc_Hide))
End Sub
'Hide Dialog during install
Private Sub Progressamc_Hide()
    progressamc.Hide()
End Sub

'Report progress 
Private Sub amcworker_ProgressChanged(ByVal sender As System.Object, ByVal e As ComponentModel.ProgressChangedEventArgs) Handles amcworker.ProgressChanged
    ProgressBarAMC.value = e.ProgressPercentage
    blprgrsAMC.Content = "Downloading: " & e.ProgressPercentage & "%"
End Sub

End Class

再次,任何帮助将不胜感激。

编辑:我已经对代码进行了以下编辑,但我不完全确定它正在做我认为它正在做的事情。基本上,我打算让 ReportProgress 每读取 2047 个字节只运行一次。

'Get the response to the Ftp request and the associated stream
    Try
        Dim response As System.Net.FtpWebResponse = CType(FTP.GetResponse, System.Net.FtpWebResponse)
        Dim Length As Long = response.ContentLength
        Dim StopWatch As New Stopwatch
        Dim CurrentSpeed As Double = Nothing
        Using responseStream As IO.Stream = response.GetResponseStream
            'loop to read & write to file
            Using fs As New IO.FileStream(localfileamc, IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Dim count As Integer
                Dim chunk As Integer = Int(2047 / Length)
                Dim cycle As Integer = chunk = count
                Do
                    If amcworker.CancellationPending = True Then
                        e.Cancel = True
                        Return
                    End If
                    StopWatch.Start()
                    If cycle = True Then
                        amcworker.ReportProgress(CShort(count / Length * 100 + 0.5))
                    Else
                        End
                    End If
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                    count += read
                Loop Until read = 0
                StopWatch.Stop()
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        response.Close()
    Catch ex As Exception
        MessageBox.Show("An error has occurred during the process:" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Please close the application and try again." _
                        & vbCrLf & "If you continue to encounter this error please Email")
        myProcessamc_Exited()
    End Try
4

1 回答 1

1

我没有仔细检查代码,但我不明白你为什么要使用秒表,所以我拿出了参考资料。我不确定在循环内多次启动它并在外面结束它会做什么。

在第二个示例中使用 END 一词将彻底结束您的应用程序!很确定这就是你想要的。

尝试对您的第一个代码进行此修改。仅当更改 >= 5% 时,密钥才会更新:

       Using fs As New IO.FileStream(localfileamc, IO.FileMode.Create)
            Dim buffer(2047) As Byte
            Dim read As Integer = 0
            Dim count As Integer
            dim LastPct as Short = -5
            dim Pct as Short = 0 
            Do
                If amcworker.CancellationPending = True Then
                    e.Cancel = True
                    Return
                End If
                Pct = CShort(count / Length * 100 + 0.5)
                if Pct>= (LastPct + 5)
                    amcworker.ReportProgress(Pct)
                    LastPCT= Pct
                EndIf
                read = responseStream.Read(buffer, 0, buffer.Length)
                fs.Write(buffer, 0, read)
                count += read
            Loop Until read = 0
            amcworker.ReportProgress(100)
            responseStream.Close()
            fs.Flush()
            fs.Close()
        End Using
于 2013-08-01T18:41:10.807 回答