BackgroundWorker 不是 运行
BackgroundWorker not running
程序逻辑
我正在制作一个简单的基于签名的恶意软件扫描程序,它可以从文件中加载文件位置。对于文件中的每一行,它将尝试获取 md5 哈希。文件中的每一行都是文件的绝对位置。
为了显示进度,我使用了进度条和后台工作程序。
问题
后台工作人员根本不是 运行ning。无论我做什么,多少次调用 worker 运行 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
额外代码
我还在检查工作程序是否在每个循环周期中被取消,以确保在单击切换按钮时,工作程序不会保持 运行ning。以下是校验函数的代码:
' 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_Scanner.IsBusy那么要像这样:如果不是bgWorker_Scanner.IsBusy那么。如果前者已经 运行ning,则前者只会 运行 BGW。
–视觉文森特
这解决了 worker no 运行ning
的主要问题
CInt(totalRead_size / file_bytes_size) * 100 应该是 CInt(totalRead_size * 100 / file_bytes_size)
– 安德鲁·莫顿
这解决了进度条的问题。
非常感谢!
程序逻辑
我正在制作一个简单的基于签名的恶意软件扫描程序,它可以从文件中加载文件位置。对于文件中的每一行,它将尝试获取 md5 哈希。文件中的每一行都是文件的绝对位置。
为了显示进度,我使用了进度条和后台工作程序。
问题
后台工作人员根本不是 运行ning。无论我做什么,多少次调用 worker 运行 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
额外代码
我还在检查工作程序是否在每个循环周期中被取消,以确保在单击切换按钮时,工作程序不会保持 运行ning。以下是校验函数的代码:
' 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_Scanner.IsBusy那么要像这样:如果不是bgWorker_Scanner.IsBusy那么。如果前者已经 运行ning,则前者只会 运行 BGW。 –视觉文森特
这解决了 worker no 运行ning
的主要问题CInt(totalRead_size / file_bytes_size) * 100 应该是 CInt(totalRead_size * 100 / file_bytes_size) – 安德鲁·莫顿
这解决了进度条的问题。
非常感谢!