Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net BackgroundWorker未运行_.net_Vb.net_Backgroundworker - Fatal编程技术网

.net BackgroundWorker未运行

.net BackgroundWorker未运行,.net,vb.net,backgroundworker,.net,Vb.net,Backgroundworker,程序逻辑 我正在制作一个简单的基于签名的恶意软件扫描器,从文件中加载文件位置。对于文件中的每一行,它将尝试获取md5哈希。文件中的每一行都是绝对文件位置 为了显示进度,我使用了progressbar和后台工作程序 问题 后台工作程序根本没有运行。无论我做什么,也不管我调用worker run asyn多少次,它似乎都不会运行 代码:用于切换按钮 Private Sub btnToggleScan_Click(sender As Object, e As EventArgs) Handles bt

程序逻辑

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

为了显示进度,我使用了progressbar和后台工作程序

问题

后台工作程序根本没有运行。无论我做什么,也不管我调用worker run 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
似乎不明白为什么它不起作用。感谢您的帮助。
下面是一个屏幕截图,以了解更多详细信息:

我想你的意思是:如果bgWorker\u Scanner.IsBusy,那么就是这样:如果不是bgWorker\u Scanner.IsBusy,那么就是这样。前者仅在BGW已在运行时运行。 –视觉效果

这就解决了工人不跑的主要问题

CInt(总读取大小/文件字节大小)*100应为CInt(总读取大小*100/文件字节大小) –安德鲁·莫顿

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
这解决了progressbar的问题


非常感谢

我想你的意思是:
如果bgWorker\u Scanner.IsBusy那么
就是这样:
如果bgWorker\u Scanner.IsBusy那么
。前者只会在BGW已经运行的情况下运行。
CInt(totalRead\u size/file\u bytes\u size)*100
应该是
CInt(totalRead\u size*100/file\u bytes\u size)
。嘿,非常感谢。我没有注意到。这就解决了问题。与其在标题中加上“已解决”,不如将答案标记为解决方案。