I have a winform application (one form), there is a RichTextBox in this form. In the constructor of this form, I create an instance of the MyClass class. In "Form_Load", I call the Initialisation method from the MyClass instance.
In the form constructor
myClass = new MyClass(RichTextBox richTextBox);
In Form_Load
myClass.Initialisation();
In the Initialisation method, in a loop, I read that some parmetologists do other things. In order not to freeze the application (because some operation may take some time, several seconds), I use BackgroundWorker . I use it like this (see code below).
When I execute, I get this error: the cross-stream operation is not valid: Control 'richTextBox access to them from the stream other than the stream on which it was created.
Could you tell me how to solve this? Work perfect when I don't get access to richTextBox
public Class MyClass { static BackgroundWorker _bw; public MyClass() { _bw = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; _bw.DoWork += bw_DoWork; _bw.ProgressChanged += bw_ProgressChanged; _bw.RunWorkerCompleted += bw_RunWorkerCompleted; } static void bw_DoWork(object sender, DoWorkEventArgs e) { foreach (....) { if (....) { richtextBox.Text.AppendText("MyText"); } } e.Result = true; } static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){} static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e){} }
c # winforms
Kris-I Jan 05 '12 at 7:00 2012-01-05 07:00
source share