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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:18+00:00 2026-05-25T10:35:18+00:00

I am using TPL to perform two tasks serially on a background thread in

  • 0

I am using TPL to perform two tasks serially on a background thread in an MVVM app. While the tasks are running, the app displays a Progress dialog. So, my MVVM command’s Execute() method first raises an ImageProcessingStarting event in the main view model. The view responds to the event by displaying the Progress dialog. The command then launches the first task, continues with the second task, and performs the final ‘continue with’ by raising an ImageProcessingEnding event in the main view model. The view responds to the event by closing the Progress dialog. The code is below.

Both background tasks are executing properly, but the Progress dialog is closing early, after the first task completes, instead of after the second one. I am hoping someone can tell me why, and how to fix the problem. Thanks for your help.


public void Execute(object parameter)
{
    ...

    // Announce that image processing is starting
    m_ViewModel.RaiseImageProcessingStartingEvent();

    // Set up a cancellation token source
    var tokenSource = new CancellationTokenSource();
    m_ViewModel.ProgressDialogViewModel.TokenSource = tokenSource;

    // Background Task #1: Add time stamps to files
    var task = Task.Factory.StartNew(() => this.AddTimeStampsToFiles(fileList, progressDialogViewModel));

    /* The Progress dialog is closing at this point! */

    // Background Task #2: Resequence files
    task.ContinueWith(t => this.ResequenceFiles(fileList, progressDialogViewModel));

    /* The Progress dialog should close at this point. */

    // Announce that image processing is finished
    task.ContinueWith(t => m_ViewModel.RaiseImageProcessingEndingEvent(), TaskScheduler.FromCurrentSynchronizationContext());
}
  • 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-25T10:35:19+00:00Added an answer on May 25, 2026 at 10:35 am

    According to MSDN

    The Task.ContinueWith Method

    Creates a continuation that executes asynchronously when the target
    Task completes.

    Which means that when the main task finishes, the other items in your two ContinueWith calls will run in parallel with one another.

    To demonstrate this, you use the code below:

    System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(() => Console.WriteLine("1"));
    task.ContinueWith((t) => { System.Threading.Thread.Sleep(1000); Console.WriteLine("2"); });
    task.ContinueWith((t) => Console.WriteLine("3"));
    

    The Output Window will then read:

    1
    3
    2
    

    To help you out in your question, I have always used the System.ComponentModel.BackgroundWorker to run tasks serially. There are probably better ways, but this works for me for now.

    public void Execute(object parameter)
    {
        BackgroundWorker bgW = new BackgroundWorker();
    
        bgW.DoWork += (s, args) =>
        {
            AddTimeStampsToFiles(fileList, progressDialogViewModel);
            ResequenceFiles(fileList, progressDialogViewModel);
        };
    
        bgW.RunWorkerCompleted += (s, args) =>
        {
            m_ViewModel.RaiseImageProcessingEndingEvent();
        }; 
    
        m_ViewModel.RaiseImageProcessingStartingEvent();
        bgW.RunWorkerAsync();
    }
    

    For this to work, you may need to pass the fileList and progressDialogViewModel values in to the bgW.RunWorkerAsync() method.

    For multiple values, I typically use a Dictionary<string, object> object so I can refer to the values by name.

    Hope this helps.

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

Sidebar

Related Questions

No related questions found

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.