程序逻辑
我正在制作一个基于签名的简单恶意软件扫描程序,该文件从文件加载文件位置。对于文件中的每一行,它将尝试获取md5哈希。文件中的每一行都是绝对文件位置。
为了显示进度,我使用了进度条和后台工作程序。
问题
后台工作程序根本没有运行。无论我做什么,以及我称其为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
其他代码
我还在检查每个循环周期中是否取消了该工作程序,以确保单击切换按钮时该工作程序不会继续运行。以下是检查功能的代码:
' 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
答案 0 :(得分:0)
我认为您的意思是:如果bgWorker_Scanner.IsBusy然后是这样:如果不是bgWorker_Scanner.IsBusy然后。前者将仅运行已运行的BGW。 –视觉Vincent
这解决了工人不奔跑的主要问题
CInt(totalRead_size / file_bytes_size)* 100应该为CInt(totalRead_size * 100 / file_bytes_size) –安德鲁·莫顿(Andrew Morton)
这解决了进度栏的问题。
非常感谢!