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 8477553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:29:56+00:00 2026-06-10T18:29:56+00:00

I know there is a similar question at: ContinueWith a Task on the Main

  • 0

I know there is a similar question at: ContinueWith a Task on the Main thread

but that question is more toward wpf and I cannot seeem to make it work on a console application.

I want to execute a method on a different thread and when that method is completed I want to keep execution on the main thread. I do not want to join method. anyways here is what I have:

class Program
{
    static void Main(string[] args)
    {
        Thread.CurrentThread.Name = "MAIN";

        DoWork(x =>
        {
            Console.Write("Method successfully executed. Executing callback method in thread:" +
                "\n" + Thread.CurrentThread.Name);
        });

        Console.Read();
    }

    static void DoWork(Action<bool> onCompleteCallback)
    {
        Console.Write(Thread.CurrentThread.Name); // show on what thred we are executing

        Task doWork = new Task(() =>
        {
            Console.Write(Thread.CurrentThread.Name); // show on what thred we are executing
            Thread.Sleep(4000);
        });

        Action<Task> onComplete = (task) =>
        {                
            onCompleteCallback(true);
        };

        doWork.Start(); 

        // this line gives an error!
        doWork.ContinueWith(onComplete, TaskScheduler.FromCurrentSynchronizationContext());                       
    }
}

How can I execute the onCompleteCallback method on the main thread?

  • 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-10T18:29:58+00:00Added an answer on June 10, 2026 at 6:29 pm

    but that question is more toward wpf and I cannot seeem to make it work on a console application.

    You can’t do this (without a lot of work) in a Console application. The mechanisms built into the TPL for marshaling the call back onto a thread all rely on the thread having an installed SynchronizationContext. This typically gets installed by the user interface framework (ie: Application.Run in Windows Forms, or in WPF’s startup code, etc).

    In most cases, it works because the main thread has a message loop, and the framework can post a message onto the message loop, which then gets picked up and runs the code. With a console application, it’s just a “raw” thread – there is no message loop where a message can be placed.

    You could, of course, install your own context, but that’s going to be adding a lot of overhead that is likely not necessary.


    In a console application, “getting back” to the console thread is typically not necessary. Normally, you’d just wait on the task, ie:

    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "MAIN";
    
            Task workTask = DoWork();
    
            workTask.Wait(); // Just wait, and the thread will continue
                             //  when the work is complete
    
            Console.Write("Method successfully executed. Executing callback method in thread:" +
                    "\n" + Thread.CurrentThread.Name);
            Console.Read();
        }
    
        static Task DoWork()
        {
            Console.Write(Thread.CurrentThread.Name); // show on what thred we are executing
    
            Task doWork = new Task(() =>
            {
                Console.Write(Thread.CurrentThread.Name); // show on what thred we are executing
                Thread.Sleep(4000);
            });
    
            doWork.Start(); 
    
            return doWork;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a very similar question here but I was hoping to
I know there's a similar question MPMusicPlayerController stops sending notifications , but the answer
I know there are a LOT of similar question to this, but I've tried
So I know that there is a very similar question to this all over
There's a similar question , I know, but it confused me, so I thought
I know there is a similar question but I took tips from there and
I know there are similar questions, but none of them answered my question. I'm
I know there are similar questions but I couldn't find something that answered my
there is a similar question in here but for some reason I cannot make
I know there are already similar question, but none of them could solve my

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.