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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:40:25+00:00 2026-06-11T09:40:25+00:00

I have the following code to build an advanced data structure which is pulled

  • 0

I have the following code to build an advanced data structure which is pulled from SQL Server, then when the retrevial of that data is complete I update the UI. The code used is

private void BuildSelectedTreeViewSectionAsync(TreeNode selectedNode)
{
    // Initialise.
    SqlServer instance = null;
    SqlServer.Database database = null;

    // Build and expand the TreeNode.
    Task task = null;
    task = Task.Factory.StartNew(() => {
        string[] tmpStrArr = selectedNode.Text.Split(' ');

        string strDatabaseName = tmpStrArr[0];

        instance = SqlServer.Instance(this.conn);

        database = instance.GetDatabaseFromName(strDatabaseName);
    }).ContinueWith(cont => {
        instance.BuildTreeViewForSelectedDatabase(this.customTreeViewSql,
            selectedNode, database);

        selectedNode.Expand();

        task.Dispose();
    }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion,
        this.MainUiScheduler);
}

This works as it should on my main development machine; that is, it completes the build of the database object, then in the continuation update the UI and disposes the task (Task object).

However, I have been doing some testing on another machine and I get an InvalidOperationException, this is due to the task.Dispose() on task which still in the Running state, but the continuation cont should never fire unless the task has ran to completion.

Here’s what the code looks like in the debugger when the exception is thrown:

enter image description here

I am aware that it almost always unneccessary to call Dispose on tasks. This question is more about why the continuation is firing at all here?**

  • 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-11T09:40:26+00:00Added an answer on June 11, 2026 at 9:40 am

    The reason for this is simple, you are calling Dispose on the continuation itself and not on the first task

    Your code consists of:

    Task task = null;
    var task = <task 1>.ContinueWith(t => { 
        /* task 2 */ 
    
        task.Dispose();
    });
    

    In the above code, task is equal to the continuation (ContinueWith doesn’t pass back the original Task, it passes the continuation) and that’s what’s getting captured in the closure you’re passing to ContinueWith.

    You can test this by comparing the references of the Task parameter passed into the ContinueWith method with task:

    Task task = null;
    var task = <task 1>.ContinueWith(t => { 
        /* task 2 */ 
        if (object.ReferenceEquals(t, task))
            throw new InvalidOperationException("Trying to dispose of myself!");
    
        task.Dispose();
    });
    

    In order to dispose of the first, you need to break it up into two Task variables and capture the first Task, like so:

    var task1 = <task 1>;
    var task2 = task1.ContinueWith(t => {
        // Dispose of task1 when done.
        using (task1)
        {
            // Do task 2.
        }
    });
    

    However, because the previous Task is passed to you as a parameter in the ContinueWith method, you don’t need to capture task in the closure at all, you can simply call Dispose on the Task passed as a parameter to you:

    var task = <task 1>.ContinueWith(t => {
        // t    = task 1
        // task = task 2
        // Dispose of task 1 when done.
        using (t)
        {
             // Do task 2.
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I have the following code, deployed on a https Asp site, build with MVC
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data
I have following code that I am compiling in a .NET 4.0 project namespace
I have an entity model build using EF4.1 code first, which uses a WCF
I have some NDK based C++ code that needs to build an android bitmap
I have a project structure that looks like the following: [Solution Name] doc (documentation)
I have the following code: function build_all_combinations(input_array){ array = [1,2,3] limit = array.length -
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

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.