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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:31:12+00:00 2026-06-12T17:31:12+00:00

I need some help with TPL and Tasks Here are my scenarios: I have

  • 0

I need some help with TPL and Tasks

Here are my scenarios:

  1. I have a few event handlers that spawn tasks. If an event is invoked before its previous task is completed, I want to wait (block) it before proceeding.

  2. I have a synchronous method that, when invoked, must wait until any spawned tasks from any event handlers are done, before proceeding.


public void DoSomething()
{
    // Expects any running tasks from OnEvent1(), OnEvent2(), OnEvent3()
    // to be completed before proceeding.
}

public void OnEvent1()
{
    Task.Factory
    .StartNew(()=>{ /*Long running task*/ })
    .ContinueWith(task=>{ /* Updates UI */ });
}

public void OnEvent2()
{
    Task.Factory
    .StartNew(()=>{ /*Long running task*/ })
    .ContinueWith(task=>{ /* Updates UI */ });
}

public void OnEvent3()
{
    Task.Factory
    .StartNew(()=>{ /*Long running task*/ })
    .ContinueWith(task=>{ /* Updates UI */ });
}

An actual scenario would be:

  • OnFetchData() => Spawns task. All subsequent calls to this needs to be queued.
  • OnSyncSettings() => Spawns task. All subsequent calls to this needs to be queued.

  • OnAutoBackup() => Synchronous method. Wait for any other tasks (e.g, Fetch data/Sync settings) to complete before saving.

  • OnFullBackup() => Synchronous method. Manually runs FetchData() and SyncSettings(), waits for completion, proceed.

My question is simply: How can I do this?

Would this approach be correct?

  1. Each event handler remembers its last . When invoked, it’ll wait for all tasks in its list to complete before proceeding.

  2. For the synchronous method, it needs to wait for all tasks (From every event handler) to


Task _lastEvent1Task;
public void OnEvent1()
{
    // Wait for all tasks to complete before proceeding
    if (_lastEvent1Task!=null)
    _lastEvent1Task.Clear();

    // Spawns new task
    _lastEvent1Task = Task.Factory.StartNew(()=>{ });
}

public void OnEvent3()
{
    // Wait for any running tasks to complete before proceeding
    if (_lastEvent1Task != null) _lastEvent1Task.Wait();
    if (_lastEvent2Task != null) _lastEvent2Task.Wait();
    ...
    // Proceed

}

Thanks!


[Edit]

When DoSomething() is waiting and Event1 is raised?
What if DoSomething() is running and an event is raised?

OnEvents()

  • If first task: Spawns task and execute asynchronously
  • If previous task still running, queue/block. Critical section.
  • Assume:
    • These events typically not raised multiple times.
    • The UI is expected to prevent user from raising the same event while still running.

DoSomething() (when called)

  • Assume: Event notifications are disabled.
  • Assume: No new events will be queued.
  • Expected: to wait until all remaining queued tasks are completed before proceeding
  • Expected: to be a synchronous call, and does not return to caller until executed through
  • 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-12T17:31:13+00:00Added an answer on June 12, 2026 at 5:31 pm

    The approach you describe has concurrency issues. For example, right after the null check on _lastEvent1Task, some other thread may set _lastEvent1Task to null, causing a NullReferenceException.

    I’m assuming that every task should run synchronously but some should not be blocking. The best way to achieve this is by using a TaskScheduler that utilizes just a single thread, you can find an example here.

    var scheduler = new SingleThreadedTaskScheduler();
    var taskFactory = new TaskFactory(scheduler);
    

    For the non-blocking methods you then get something like this:

    public void NonBlockingMethod()
    {
        taskFactory
            .StartNew(() => { /* Long-running work. */ })
            .ContinueWith(t => { /* Update UI. */ });
    }
    

    For the blocking methods you get:

    public void BlockingMethod()
    {
        taskFactory
            .StartNew(() => { /* Do work. */ })
            .Wait();
    }
    

    When you schedule tasks, the task scheduler will use just one thread for scheduling them, guaranteeing that all tasks run synchronously.

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

Sidebar

Related Questions

Need some help assigning a mouseover event to display some icons that start out
Need some help with DataFormatString in GridView. I have a Double value that needs
Need some help, I have a regular expression that appears to work just fine
Need some help joining these two tables I have two views that looks like
Need some help here. I have deployed spree (0.70.3) on slicehost (ubuntu, ruby1.8.7, Rails
Just need some help with a SQL statement, I have a table that has
I need some help. Basically, I have a large dataset that I have split
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.