Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:54:24+00:00 2026-06-04T14:54:24+00:00

I have an asynchronously running Task that fires an event when it’s completed like

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:54:26+00:00Added an answer on June 4, 2026 at 2:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an asynchronous task that performs a potentially long-running calculation for an object.
I have an Access 2007 database that works asynchronously with a MAS200 database. Due
I have a method which I would like to invoke asynchronously: void Foo() {
I have a UITableView whose cells contain custom ImageViews that asynchronously load the images
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have some records, that I want to save to database asynchronously. I organize
I have 2 threads running a chat program such that one is a server
I have a task that might take a while to complete. I have tried
I have a basic asynchronous task that performs a web request. The thread is
I have the following code which I want to be running asynchronously, but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.