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

  • Home
  • SEARCH
  • 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 8858677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:52:56+00:00 2026-06-14T14:52:56+00:00

I want to implement the following functionality in C# console application: spawn several worker

  • 0

I want to implement the following functionality in C# console application:

  1. spawn several worker threads in main thread;
  2. each worker thread processes data and send string message to main thread periodically;
  3. main thread processes messages and wait for all worker threads to finish;
  4. main thread exit.

Now I’m using TPL, but I don’t know how to send messages from worker threads to main thread. Thank you for help!

  • 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-14T14:52:58+00:00Added an answer on June 14, 2026 at 2:52 pm

    You could use a Producer / Consumer pattern with the TPL as demonstrated in this example on an MSDN blog.

    Or, you could kick it old-school and use signaling, see AutoResetEvent.

    Or if you care to work at a very low level, use Monitor.Pulse with Monitor.WaitOne (as demonstrated here).

    Either way, you are looking for Synchronization, which you can read up on here.


    Other option, if you don’t actually care what thread the update is running on, would be to take a delegate as an argument and print the update there, à la:

    static Task<string> ReadFile(string filename, Action<string> updateStatus)
    {
    
        //do stuf synchronously
        return Task<string>.Factory.StartNew(() => {
            System.Threading.Thread.Sleep(1000);
            //do async stuff
            updateStatus("update message");
            //do other stuff
            return "The result";
        });
    }
    
    public static void Main(string[] args) 
    {
        var getStringTask = ReadFile("File.txt", (update) => {
            Console.WriteLine(update)
        });
        Console.Writeline("Reading file...");
        //do other stuff here
        //getStringTask.Result will wait if not complete
        Console.WriteLine("File contents: {0}", getStringTask.Result);
    }
    

    would print:

    Reading file...
    update message
    File contents: The result
    

    The "update message" call to Console.WriteLine would still occur on the ThreadPool thread, but it may still fill your need.

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

Sidebar

Related Questions

I am new to multi-thread programming, I want to implement the following functionality. There
In an application having UITabBarController , I want to implement following scenario: When user
I want to implement the screen lock functionality in my application, but currently I
I want to implement following functionality but I am confused if it's possible in
I want the following redirection functionality GET /google google.com How should I implement this?
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
I want to implement an application which will work as a parser. User will
I'm new to WPF but I need to implement following functionality: I have a
Because I didn't want to implement a communication protocol for my client-server based application,
I'm trying to use metaclasses to implement the following functionality: class foo( object ):

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.