2

I have an asynchronously running Task that fires an event when it's completed like this:

task.ContinueWith(() => {
    if (MyEvent != null)
        MyEvent(this, EventArgs.Empty);
}

The event handler then should create an instance of a WPF control. But when I try to do so, it causes an exception: The calling thread must be STA, because many UI components require this. Exception occurs in the class constructor, when calling method InitializeComponent().

As far as I know, usually accessing WPF controls from separate threads is handled using the Dispatcher.Invoke, and it always worked for me, so I tried it:

Dispatcher.Invoke(new Action(() =>
{
    InitializeComponent();
}));

But in that case exception keeps occurring. How do I create an instance of a WPF control from a separate thread?

Or maybe it will be a better approach to marshal the completion event to the main UI thread. If yes, how can I do that?

3 Answers 3

3

You have to use a Dispatcher instance, which was associated with the UI thread. If you are writing something like this:

Dispatcher.Invoke(new Action(() =>
{
    InitializeComponent();
}));

In the task body, you're using dispatcher of the calling thread, which can be a background thread from a pool.

Anyway, with tasks you shouldn't use Dispatcher directly. Use an appropriate task scheduler:

var ui = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.ContinueWhenAll(tasks.ToArray(),
    result =>
    {
        // Put you UI calls here

    }, CancellationToken.None, TaskContinuationOptions.None, ui);

where tasks is a sequence of tasks being executed with the default scheduler.

Sign up to request clarification or add additional context in comments.

Comments

1

Calling InitializeComponent from the constructor on another thread seems like looking for trouble. The object isn't there yet (we're in the constructor)

Marshaling it back to the UI thread would normally do the trick but during the constructor looks like a bad idea to me.

If you want to initialize the control asynchronously just subscribe to the loaded event, so you know the object is there, spawn a thread that does some calculations/data retrieval and marshals the data back to the UI thread to display it.

Comments

1

I have done this in the past:

 Dispatcher.Invoke(DispatcherPriority.Normal,
                   new Action(
                        delegate()
                        {
                            // Access control created by main thread
                            textBlock.Text = msg;
                        }
                   ));

3 Comments

I would use DispatcherPriority.Render since we are creating a Control.
I have been able to add controls to a parent this way at least... EX: panel.Children.Add(new MyControl()) etc
Thanks, but Dispatcher.Priority changed nothing in my case (

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.