Well, I have the following problem and I hope that you can help me:
I would like to create a WPF application with a background worker for updating richtextboxes and other UI elements. This background worker should process some data, e.g. handle the content of a folder, do some parsing and much more. Since I would like to move as much code as possible outside the Main class, I created a class called MyProcess.cs
as you can see below (in fact, this class does not make much sense so far, it will be filled with much more processing elements if this problem has been solved). The general functionality should be:
- MainWindow: An array of strings will be created (named
this.folderContent
) - MainWindow: A background worker is started taking this array as argument
- MainWindow: The
DoWork()
method will be called (I know, this one now runs in a new thread) - MyProcess: Generates a (so far unformatted) Paragraph based on the given string array
- MainWindow: If the background worker is finished, the
RunWorkerCompleted()
method is called (running in UI thread) which should update a WPF RichTextBox via the method's return argument
This last step causes an InvalidOperationsException with the note, that "The calling thread cannot access this object because a different thread owns it." I read a bit about the background worker class and its functionality. So I think that it has something to do with the this.formatedFilenames.Inlines.Add(new Run(...))
call in the Execute()
method of MyProcess
. If I replace the Paragraph attribute by a list of strings or something similar (without additional new()
calls) I can access this member without any problems by a get method. All examples related to the background worker I have found return only basic types or simple classes.
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
this.process = new MyProcess();
this.worker = new BackgroundWorker();
this.worker.DoWork += worker_DoWork;
this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
this.process.Execute((string[])e.Argument);
e.Result = this.process.Paragraph();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.rtbFolderContent.Document.Blocks.Clear();
// the next line causes InvalidOperationsException:
// The calling thread cannot access this object because a different thread owns it.
this.rtbFolderContent.Document.Blocks.Add((Paragraph)e.Result);
}
...
// folderContent of type string[]
this.worker.RunWorkerAsync(this.folderContent);
...
Edit: Since this has been asked: the RunWorkerAsync is called e.g. on a button click event or after a folder has been selected via a dialog, so within the UI thread.
MyProcess.cs
class MyProcess
{
Paragraph formatedFilenames;
public MyProcess ()
{
this.formatedFilenames = new Paragraph();
}
public void Execute(string[] folderContent)
{
this.formatedFilenames = new Paragraph();
if (folderContent.Length > 0)
{
for (int f = 0; f < folderContent.Length; ++f)
{
this.formatedFilenames.Inlines.Add(new Run(folderContent[f] + Environment.NewLine));
// some dummy waiting time
Thread.Sleep(500);
}
}
}
public Paragraph Paragraph()
{
return this.formatedFilenames;
}
}