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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:19:49+00:00 2026-06-08T02:19:49+00:00

I have a task which, when completed, is supposed to continue with another task

  • 0

I have a task which, when completed, is supposed to continue with another task that shows a winform (the winform was previously initialised on the UI thread, so it does have a handle).

    private static Task RunningTask
    {
        get;
        set;
    }
    public static UpdaterTool.Forms.UpdateResult UpdateResultForm;

    private void DoWork()
    {
        UpdateResultForm = new Forms.UpdateResult(); 
        //the next line forces the creation of the handle - 
        //otherwise InvokeRequired will later on return false.
        var hackHandle = UpdateResultForm.Handle; 

        var ctx = TaskScheduler.FromCurrentSynchronizationContext();

        RunningTask = Task.Factory.StartNew(() => DownloadAndInstallFiles(), CancelTokenSource.Token)
            .ContinueWith(_ => WorkComplete(), CancelTokenSource.Token, TaskContinuationOptions.NotOnFaulted, ctx);
    }

    private void WorkComplete()
    {
       ShowResultForm();
    }

    private void ShowResultForm()
    {
        if (UpdateResultForm.InvokeRequired)
        {
            try
            {
                UpdateResultForm.Invoke(new MethodInvoker(ShowResultForm));
            }
            catch { }
            return;
         }
         UpdateResultForm.Show();

     }

The problem is that no matter what combination of overloads for the ContinueWith() I use, the UpdateResultForm is either not shown at all(meaning the continuation does not happen, the worker hangs at “running”), or when it is, it hangs the UI, like its expecting the worker thread to finish or something. I dont understand why this happens when I tried to show it on the UI thread, by using FromCurrentSynchronizationContext().

In my understanding, inside the DoWork method I start on the UI thread (which is why I initialise the form there). When the code enters Task.Factory.StartNew, it switches to the working thread. When it completes, it continues with WorkComplete which just shows the previously initialised form, on the UI thread.

What am I missing? Thanks,

  • 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-08T02:19:54+00:00Added an answer on June 8, 2026 at 2:19 am

    Using InvokeRequired is a strong anti-pattern. It’s not that common to have no idea on what thread a method runs. That’s the case here as well, you already used TaskScheduler.FromCurrentSynchronizationContext() so you know that the task runs on the UI thread. There’s no point in checking again. Which also eliminates the need to create the form handle early.

    The kind of problems you are having can be caused by running DoWork() from a worker thread. Maybe you called it from a task as well. Which makes FromCurrentSynchronizationContext() return the wrong context. And will display the form on a thread that doesn’t pump a message loop, it will be dead as a doornail. Or by blocking the UI thread, waiting for the task to finish. That will always cause deadlock, the task cannot complete unless the UI thread goes idle and can execute the ShowResultForm() method.

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

Sidebar

Related Questions

I have a javascript function which supposed to check whether a task is completed.
I had an pre-interview task, which I have completed and the solution works, however
I have a task/mini project to complete which is along the lines of GUI
i have task which takes a parameter and has three modes of results Example
I have a task which has to be run everyday. I will get just
I have a model Task which will HABTM many TaskTargets. When it comes to
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
I have an interesting scenario. I have a task model which has a task
This is a simplified task which I have to solve in real project. In
I have a task to develop an electronic phone book in which i have

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.