vb.net 状态栏文本不显示



我有一个代码,用于搜索文档中的单词,并用找到的文档填充Listview。因为这个过程可能相当长,所以我在状态栏中发出了警告。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim foundList As Boolean
Dim docImage As VariantType
Dim PresetName As String
ListView1.Items.Clear()
ToolStripLabel1.Text = "Searching your documents, please wait."
Try
For Each row As DataRowView In CheckedListBox1.CheckedItems
SearchRegX = row("RegX")
Next
If TextBoxFreeText.Text <> "" Then
'look for a space in the search criteria, meaning there are more words
Dim counter As Integer = TextBoxFreeText.Text.IndexOf(" ")
If counter <> -1 Then
'more words were entered to search
Dim varSplit As Object
varSplit = Split(TextBoxFreeText.Text, " ")
'if more than two words are entered our regex doesnt work, so we exit the sub
If varSplit.length > 2 Then
MsgBox("Your search criteria are to complex, use a maximum of two words",, Title)
Exit Sub
End If
iWords = NumericUpDown1.Value
SearchRegX = "(?i)b(?:" + varSplit(0) + "W+(?:w+W+){0," + iWords + "}?" + varSplit(1) + "|" + varSplit(1) + "W+(?:w+W+){0," + iWords + "}?" + varSplit(0) + ")b"
Else
'just one word was entered
SearchRegX = "(?i)b" + TextBoxFreeText.Text + "b"
End If
End If
If SearchRegX = "" Then
MsgBox("No Keyword was selected",, Title)
Exit Sub
End If
If ListBox1.SelectedIndex > -1 Then
For Each Item As Object In ListBox1.SelectedItems
Dim ItemSelected = CType(Item("Path"), String)
SearchFolder = ItemSelected
'check if the folder of the archive still exists
If (Not System.IO.Directory.Exists(SearchFolder)) Then
Dim unused = MsgBox("The archive " + SearchFolder.Substring(SearchFolder.Length - 5, Length) + " was not found",, Title)
Continue For
End If
Dim dirInfo As New IO.DirectoryInfo(SearchFolder)
Dim files As IO.FileInfo() = dirInfo.GetFiles()
Dim file As IO.FileInfo
docImage = ImageList1.Images.Count - 1
Dim items As New List(Of ListViewItem)
For Each file In files
Dim filename As String = file.Name.ToString
If file.Extension = ".pdf" Or file.Extension = ".PDF" Then
foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
If foundList = True Then
If ListView1.FindItemWithText(filename.ToString) Is Nothing Then
items.Add(New ListViewItem(New String() {"", filename.ToString, SearchFolder.ToString}, docImage))
End If
End If
End If
Next
ListView1.Items.AddRange(items.ToArray)
Next
ToolStripLabel1.Text = ListView1.Items.Count.ToString + " Documents found."
Else
MsgBox("No archive was selected",, Title)
End If
SearchRegX = ""
SearchFolder = ""
'now save the search word to our textfile
PresetName = TextBoxFreeText.Text
If PresetName <> "" Then
AddSearchWord()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

不知怎的,在流程开始之前,文本不会显示在状态栏中。

我应该更改什么?我曾想过BackgroundWorker显示一个类似等待的图像,但BackgroundWorker不起作用,因为在我的Sub中,我在其他地方调用了一个函数。

这是因为在挂起的工作负载完成之前,状态条控件没有正确呈现。为此,您可以手动刷新状态条:

StatusStrip1.Refresh()

也就是说,如果你的唯一目标是设置文本。如果您考虑在后台运行代码,以便表单仍然响应用户输入,则需要使用System.Threading.TasksSystem.Threading.Thread进行异步编程,以将代码作为单独的线程运行。但是要注意,在尝试访问主线程之外的控件时可能会遇到困难。

Async添加到按钮处理程序声明中:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

现在将您的呼叫更改为PDFManipulation.GetTextFromPDF2(),使其处于任务范围内:

Await Task.Run(Sub()
foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
End Sub)
If foundList = True Then
...

最新更新