表格只是冻结循环通过行
The form just freeze looping throught lines
我只是陷入了无法以任何方式解决的问题中。即使使用 BackgroundWorker,我的 UI 也会冻结。
关于我解决的老问题:
'我在文本文件中有这种格式的行:
word1|word2|word3
anotherword1|anotherword2
我正在尝试将该文件的每一行一个一个地拆分每个单词,一旦程序检测到 richtextbox 是否包含这些单词之一,就会用未拆分的行替换该单词。示例:从 word1 到 word1|word2|word3'
一切都很好,但前提是我使用的文件只有一小部分要拆分的行。但是我要一下子分一大块。
这是我目前的情况:http://pastebin.com/raw/k0MtPHbZ
正如我所说,如果我减少 en.txt 文件的行数,一切都会正常,我有点困惑为什么。如果有人能告诉我如何解决这个问题,我将不胜感激。
更新:
正如你们所说,看我做了什么:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim list As New List(Of String)()
Using reader As New StreamReader(Application.StartupPath & "\en.txt")
Dim line As String = Nothing
Dim input = RichTextBox1.Text
While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
Dim pat = String.Format("\b({0})\b", line)
input = Regex.Replace(input, pat, line)
End While
RichTextBox2.Text = input
End Using
End Sub
但还是一样。少量线条工作正常。冻结我的 500kb 文本文件。
您的 Pastebin link 包含以下代码:
For i = 0 To 100
Threading.Thread.Sleep(200)
[...]
Next
乍一看,为什么要让线程在 for 循环的每次迭代中休眠五分之一秒?
删除初学者的这一行。
我相信您的后台工作人员仍会阻塞您的 UI 线程,因为您在 DoWork 部分引用 UI 控件。您最好在 UI 线程上提取数据,将其分配给一个变量,然后在 DoWork 的内存中处理所有数据,而不是尝试从后台线程操作 UI,这会一直给你带来悲伤。
因此,在您的 button1.Click 处理程序中,从文本框中获取输入并将其分配给实例变量。在您的 DoWork 中引用该实例变量作为输入。
示例:
Public Class Form1
Private _textInput As String = String.Empty
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
_textInput = RichTextBOx1.Text
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 To 100
Threading.Thread.Sleep(200)
Dim list As New List(Of String)()
Using reader As New StreamReader(Application.StartupPath & "\en.txt")
Dim line As String = Nothing
While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
Dim pat = String.Format("\b({0})\b", line)
_textInput = Regex.Replace(_textInput , pat, line)
End While
End Using
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("done")
RichTextBox1.Text = _textInput
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
我只是陷入了无法以任何方式解决的问题中。即使使用 BackgroundWorker,我的 UI 也会冻结。
关于我解决的老问题:
'我在文本文件中有这种格式的行:
word1|word2|word3
anotherword1|anotherword2
我正在尝试将该文件的每一行一个一个地拆分每个单词,一旦程序检测到 richtextbox 是否包含这些单词之一,就会用未拆分的行替换该单词。示例:从 word1 到 word1|word2|word3'
一切都很好,但前提是我使用的文件只有一小部分要拆分的行。但是我要一下子分一大块。
这是我目前的情况:http://pastebin.com/raw/k0MtPHbZ
正如我所说,如果我减少 en.txt 文件的行数,一切都会正常,我有点困惑为什么。如果有人能告诉我如何解决这个问题,我将不胜感激。
更新:
正如你们所说,看我做了什么:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim list As New List(Of String)()
Using reader As New StreamReader(Application.StartupPath & "\en.txt")
Dim line As String = Nothing
Dim input = RichTextBox1.Text
While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
Dim pat = String.Format("\b({0})\b", line)
input = Regex.Replace(input, pat, line)
End While
RichTextBox2.Text = input
End Using
End Sub
但还是一样。少量线条工作正常。冻结我的 500kb 文本文件。
您的 Pastebin link 包含以下代码:
For i = 0 To 100
Threading.Thread.Sleep(200)
[...]
Next
乍一看,为什么要让线程在 for 循环的每次迭代中休眠五分之一秒?
删除初学者的这一行。
我相信您的后台工作人员仍会阻塞您的 UI 线程,因为您在 DoWork 部分引用 UI 控件。您最好在 UI 线程上提取数据,将其分配给一个变量,然后在 DoWork 的内存中处理所有数据,而不是尝试从后台线程操作 UI,这会一直给你带来悲伤。
因此,在您的 button1.Click 处理程序中,从文本框中获取输入并将其分配给实例变量。在您的 DoWork 中引用该实例变量作为输入。
示例:
Public Class Form1
Private _textInput As String = String.Empty
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
_textInput = RichTextBOx1.Text
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 To 100
Threading.Thread.Sleep(200)
Dim list As New List(Of String)()
Using reader As New StreamReader(Application.StartupPath & "\en.txt")
Dim line As String = Nothing
While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
Dim pat = String.Format("\b({0})\b", line)
_textInput = Regex.Replace(_textInput , pat, line)
End While
End Using
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("done")
RichTextBox1.Text = _textInput
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class