It is hard to figure out what exactly you're trying to do. Here is how you can initiate the updating of the UI from your worker thread in a way that actually updates it on the UI thread, avoiding the error:
public partial class MainWindow : Window
{
BackgroundWorker Worker;
public MainWindow()
{
InitializeComponent();
Worker = new BackgroundWorker();
Worker.DoWork += new DoWorkEventHandler(worker_DoWork);
Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
Worker.RunWorkerAsync("param");
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
tb.Dispatcher.Invoke((Action)(() =>
{
tb.Text = "doing work: " + (e.Argument as string);
}));
Thread.Sleep(5000);
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
tb.Dispatcher.Invoke((Action)(() =>
{
tb.Text = "finished work";
}));
}
}
Notice that since I am updating the UI from the background thread, I don't use CurrentDispatcher, I use the dispatcher in charge of the thread that owns the control I am updating (tb is a TextBox in the XAML file of the window).
I am not sure what to replace your CurrentThread.Join with as I don't understand the point of that part of the code. But CurrentThread.Join is definitely wrong, it's basically a self-inflicted deadlock on a thread.