BackgroundWorker未运行

问题描述 投票:-1回答:1

程序逻辑

我正在制作一个简单的基于签名的恶意软件扫描程序,它从文件中加载文件位置。对于文件中的每一行,它将尝试获取md5哈希值。文件中的每一行都是绝对文件位置。

为了显示进度,我正在使用进度条和后台工作程序。

问题

后台工作者根本没有运行。无论我做什么,多少次我打电话给工人运行asyn,它似乎没有运行。

代码:用于切换按钮

Private Sub btnToggleScan_Click(sender As Object, e As EventArgs) Handles btnToggleScan.Click
    If isScanning = True Then
        ' if scanning, stop scanning
        txtStatus.Text = "Status: Idle..."
        btnToggleScan.Image = Image.FromFile("res/malware_scanner/rocket.png")
        If bgWorker_Scanner.IsBusy Then
            Try
                bgWorker_Scanner.CancelAsync()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If
        isScanning = False
    Else
        ' if not scanning, start scanning
        txtStatus.Text = "Status: Scanning..."
        btnToggleScan.Image = Image.FromFile("res/malware_scanner/loading_dark.gif")
        If bgWorker_Scanner.IsBusy Then
            Try
                bgWorker_Scanner.RunWorkerAsync()
            Catch ex As Exception
                Me.Close()
            End Try
        End If
        txtCalmDown.Text = "Feel free to do other work!"
        isScanning = True
    End If
End Sub

代码:适用于后台员工

Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
        ' storage declares
        Dim temp_hash_values As New List(Of String)() ' store malware hash per malware hash file
        Dim hashFile_lineParts As String() ' store parts of packed/unpacked hash
        Dim se_queryfile_hashes As New List(Of String)() ' store file hashes of search index query

        ' file operating declares
        Dim file_bytes() As Byte
        Dim file_bytes_size As Integer
        Dim lineInFile As String
        Dim lineBytes() As Byte
        Dim lineBytes_size As Integer
        Dim totalRead_size As Integer

        ' declare file reader
        Dim reader As StreamReader

        ' if quickscan, then get hash list
        If scanType = "Quick" Then
            Dim md5hash As String
            reader = My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "/data/win_searchIndex_results.list")
            ' get file bytes
            file_bytes = File.ReadAllBytes(Application.StartupPath & "/data/win_searchIndex_results.list")
            ' get size of file bytes in integer
            file_bytes_size = file_bytes.Length
            Do
                lineInFile = reader.ReadLine
                If Not String.IsNullOrEmpty(lineInFile) Then
                    ' get line bytes
                    lineBytes = System.Text.Encoding.ASCII.GetBytes(lineInFile)
                    ' get line bytes size in integer
                    lineBytes_size = lineBytes.Length
                    ' add line bytes size to total size read
                    totalRead_size += lineBytes_size
                    Try
                        md5hash = Hasher.Getmd5(lineInFile)
                        se_queryfile_hashes.Add(md5hash.ToString)
                        ' testing
                        ' Dim temp As String = totalRead_size.ToString & " \ " & file_bytes_size.ToString
                        ' MsgBox(md5hash.ToString)
                    Catch ex As Exception : End Try
                End If
                bgWorker_Scanner.ReportProgress(CInt(totalRead_size / file_bytes_size) * 100)
                check_bgWorkerCancelled()
            Loop Until lineInFile Is Nothing
        End If

' clean temporary storage after each file operation
        temp_hash_values.Clear() : Erase hashFile_lineParts : Erase file_bytes : file_bytes_size = 0 : lineInFile = Nothing : Erase lineBytes : lineBytes_size = Nothing

    End Sub

额外代码

我还要检查工作程序是否在每个循环周期中被取消,以确保在单击切换按钮时,工作程序不会继续运行。以下是检查功能的代码:

' Method: To check is cancellation is pending
    Private Sub check_bgWorkerCancelled()
        ' check if cancellation is pending
        If bgWorker_Scanner.CancellationPending = True Then
            ' background worker cancel asynchronoous operating
            If bgWorker_Scanner.IsBusy Then
                bgWorker_Scanner.CancelAsync()
            End If
            isScanning = False
            Try
                ' invoke to bypass illegal cross threading UI update
                BeginInvoke(CType(Sub()
                                      progressBar1.Value = 0
                                      txtStatus.Text = "Cancelled"
                                      txtCalmDown.Text = ""
                                      btnToggleScan.Image = Image.FromFile(Application.StartupPath & "/res/malware_scanner/rocket.png")
                                  End Sub, MethodInvoker))
            Catch ex As Exception : End Try
        Else
            Exit Sub
        End If
    End Sub

似乎无法弄清楚为什么它没有踢。任何帮助都表示赞赏。以下是更多详细信息的屏幕截图:Runtime Image

.net vb.net backgroundworker
1个回答
0
投票

我认为你的意思是:如果bgWorker_Scanner.IsBusy那么这样:如果不是bgWorker_Scanner.IsBusy然后。如果BGW已经运行,前者只会运行BGW。 - 视觉文森特

这解决了工人没有跑步的主要问题

CInt(totalRead_size / file_bytes_size)* 100应为CInt(totalRead_size * 100 / file_bytes_size) - Andrew Morton

这解决了进度条的问题。

非常感谢!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.