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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:53:22+00:00 2026-05-25T22:53:22+00:00

I am working with TPL and need to have a long running TPL task

  • 0

I am working with TPL and need to have a long running TPL task send results to the parent UI thread without terminating. I have tried several approaches and have been googling quite a bit. Does anyone know how to make this happen with TPL?

  • 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-05-25T22:53:23+00:00Added an answer on May 25, 2026 at 10:53 pm

    You could pass in a delegate to call with periodic results, and a SynchronizationContext which the task could use to invoke the callback on the correct thread. That’s basically the way that BackgroundWorker does it (and the way that the async feature of C# 5 will “know” where to call you back) – it captures SynchronizationContext.Current on the calling thread, then calls Post (IIRC) to post a message to the right context. You then just need to wrap the original callback in a SendOrPostCallback which executes it when it’s got to the right thread.

    EDIT: Sample program:

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using System.Threading.Tasks;
    
    class Test
    {
        static void Main()
        {
            Form form = new Form();
            Label label = new Label();
            form.Controls.Add(label);
            form.Load += delegate { HandleLoaded(label); };
            Application.Run(form);
        }
    
        static void HandleLoaded(Label label)
        {
            Action<string> callback = text => label.Text = text;
            StartTask(callback);
        }
    
        static void StartTask(Action<string> callback)
        {
            SendOrPostCallback postCallback = obj => callback((string) obj);
            SynchronizationContext context = SynchronizationContext.Current;
    
            Task.Factory.StartNew(() => {
                for (int i = 0; i < 100; i++)
                {
                    string text = i.ToString();
                    context.Post(postCallback, text);
                    Thread.Sleep(100);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've my working on using the TPL in C# (.NET 4.0). I have created
I have created a tpl file for render the webform node.webform is working fine
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Working on a project at the moment and we have to implement soft deletion
on the site I'm working on, I need to show two different HTML snippets
I'm using the TPL ( Task Parallel Library ) in .NET 4.0. I want
I am working on a project that deals with a few tasks running in
I have been working with Struts for some time, but for a project I
I'm working with the MVC example here , and have bypassed the PhoneGap contacts
I'm working through Practical Web 2.0 Appications currently and have hit a bit of

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.