Hi, I'm using VB.NET2010 (VS2010) WinForms. I have a method called "parseFile()" that parses a TXT file and writes to a Textbox Control as it performs a calculation on each row of dataTable, which I create within that method. -- All that works well. (No problems so far...)
I then tried to add a BackgroundWorker, so that I can see the progress of my method as it parses the TXT file and so that I can add a progress bar to my Form, thus allowing me to let users know how far along the calculations are going.
However, once I added the BackgroundWorker, I get this error:
>> Cross-thread operation not valid: Control 'tbxOutPut' accessed from a thread other than the thread it was created on.
Note: ("tbxOutPut" is my Textbox Control)
The Texbox was created in the designer by adding a Textbox Control to the Form.
Below is the code that calls the BackgroundWorker. The line of code it stops on is below, which is the first time I access the Textbox Control once the method has begun.
>> tbxOutPut.Clear() <--- I get the error here in my code
I know that I'm accessing one thread from within another. I know that is unsafe. However, I don't know how to do it in a "safe" manner - other than rewriting all my code and splitting the Textbox access to another method, which is not something trivial - nor something I'm willing to do, so I would really like to find a way to correct this error.
I tried also to look this up and found this post, but I'm still confused.
Private Sub btnParseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParseFile.Click
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork