none
Application.DoEvents() 似乎无效 RRS feed

  • 问题

  • 大家好。我是业余的程序设计爱好者,以下问题描述尚欠专业,请见谅。

    我想自编一个文件 MD5 值计算器。当文件体积大的话,程序运行就会出现类似停止响应的情况,直至程序运行结束。分析了一下,就是 ComputeHash 函数运行的结果,程序运行的时间大部份都花在这句代码上。

    于是,添加了 Application.DoEvents() 语句,可是,效果没什么改变。原本想添加一个 ProgressBar 来显示进度,可
    ProgressBar 也是一直停止响应,直至程序运行结束,然后显示满格。

    我该怎么处理为好?谢谢大家出出主意。祝好!

    =========================================

    模块代码如下:

    Module mdlCalculateMD5

        Public Function MD5CalcFile(ByVal strPath As String) As String
            Dim objReader As System.IO.Stream
            Dim objMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim arrHash() As Byte
            objReader = New System.IO.FileStream(strPath, IO.FileMode.Open, IO.FileAccess.Read)
            Application.DoEvents()
            arrHash = objMD5.ComputeHash(objReader)
            objReader.Close()
            objReader = Nothing
            objMD5 = Nothing
            Return ByteArrayToString(arrHash)
        End Function

        Public Function ByteArrayToString(ByVal arrInput() As Byte) As String
            Dim strOutput As New System.Text.StringBuilder(arrInput.Length)
            For i As Integer = 0 To arrInput.Length - 1
                Application.DoEvents()
                strOutput.Append(arrInput(i).ToString("X2"))
            Next
            Return strOutput.ToString().ToLower
        End Function

    End Module

    窗体代码如下:

    Public Class frmMain

        Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            ofdCalculateMD5.ShowDialog()
            Application.DoEvents()
            txtPath.Text = ofdCalculateMD5.FileName
        End Sub

        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim md5hash As String
            md5hash = MD5CalcFile(txtPath.Text)
            txtMD5.Text = md5hash
            MsgBox("Done!", MsgBoxStyle.Information, "MD5Calculator")
        End Sub

    End Class
    2008年11月24日 13:03

答案

  • 问题已解决。

    利用 BackgroundWorker 控件。

    窗体代码略作更改:

    Public Class frmMain

        Dim md5hash As String

        Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            ofdCalculateMD5.ShowDialog()
            Application.DoEvents()
            txtPath.Text = ofdCalculateMD5.FileName
        End Sub

        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

            bgwComputeHash.RunWorkerAsync()

        End Sub

    Private Sub bgwComputeHash_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwComputeHash.DoWork
            md5hash = MD5CalcFile(txtPath.Text)
        End Sub

    Private Sub bgwComputeHash_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwComputeHash.RunWorkerCompleted
            txtMD5.Text = md5hash
            MsgBox("Done!", MsgBoxStyle.Information, "MD5Calculator V1.0")
        End Sub

    End Class

    2008年11月25日 1:24

全部回复

  • objMD5.ComputeHash(objReader)
    应该用另一个线程来做

    2008年11月25日 1:18
    版主
  • 问题已解决。

    利用 BackgroundWorker 控件。

    窗体代码略作更改:

    Public Class frmMain

        Dim md5hash As String

        Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            ofdCalculateMD5.ShowDialog()
            Application.DoEvents()
            txtPath.Text = ofdCalculateMD5.FileName
        End Sub

        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

            bgwComputeHash.RunWorkerAsync()

        End Sub

    Private Sub bgwComputeHash_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwComputeHash.DoWork
            md5hash = MD5CalcFile(txtPath.Text)
        End Sub

    Private Sub bgwComputeHash_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwComputeHash.RunWorkerCompleted
            txtMD5.Text = md5hash
            MsgBox("Done!", MsgBoxStyle.Information, "MD5Calculator V1.0")
        End Sub

    End Class

    2008年11月25日 1:24