Reputation: 121
I'm just stuck into something in which I can't solve it in any way. My UI freeze even with BackgroundWorker.
Regarding to my old solved problem: VB.NET - It keep replacing itself
'I have in a text file lines of this format:
word1|word2|word3
anotherword1|anotherword2
I'm trying to split each word one by one per every line of that file and once program detect if the richtextbox has one of these words will replace that word with the unsplitted line. Example: From word1 to word1|word2|word3'
Everything works great, but only if I'm using a file with a small set of lines to split. But I need to split a big one at once.
Here is what I have so far: http://pastebin.com/raw/k0MtPHbZ
As I said, everything works if I reduce the lines of the en.txt file and I'm kinda confused why. I would really appreciate if someone would tell me how to fix this problem.
UPDATE:
As you guys said look what I did:
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
But it still does the same. Work fine with small amount of lines. Freeze with my 500kb text file.
Upvotes: 2
Views: 132
Reputation: 1332
I believe your background worker is still going to block on your UI thread because you're referencing UI controls in the DoWork portion. You would be better off pulling in the data on the UI thread, assigning it to a variable, and then processing that all in memory in the DoWork instead of attempting to manipulate UI from a background thread, this is going to give you grief consistently.
So in your button1.Click handler, get the input from the textbox and assign it to an instance variable. Reference that instance variable inside your DoWork for the input.
Example:
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
Upvotes: 1
Reputation: 2322
Your Pastebin link includes the following code:
For i = 0 To 100
Threading.Thread.Sleep(200)
[...]
Next
At a glance, why are you putting the thread to sleep for a fifth of a second on every iteration of the for-loop?
Remove this line for starters.
Upvotes: 1