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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:01:58+00:00 2026-05-27T13:01:58+00:00

What happens to a local Task reference when it runs out of scope and

  • 0

What happens to a local Task reference when it runs out of scope and the garbage collector decides to get rid of it before the task completes?

Basically I’m asking if it’s safe to make this kind of service call implementation:

/// <summary>
/// Web service operation to start a large batch asynchronously
/// </summary>    
public StartBatchResponseMessage StartBatch(StartBatchRequestMessage request)
{
  Task t = Task.Factory.StartNew(DoWork);
  return new StartBatchResponseMessage();
}

private void DoWork()
{
  // Implement solving world hunger here.
}

Will DoWork always complete in this example? Even if the garbage collector decides to dispose the Task instance variable t?

If not, what is a safer way to implement this kind of functionality?

  • 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-27T13:01:59+00:00Added an answer on May 27, 2026 at 1:01 pm

    yes, it will be run. If you decompile your code you can see the call to Task.Factory.StartNew calls Task.InternalStartNew, which calls Task.ScheduleAndStart(false).

    This in turn calls task.m_taskScheduler.QueueTask(this), which according to MSDN adds the task to an internal structure – which will of course stop it from being garbage collected. Which is exactly as you’d hope.

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

Sidebar

Related Questions

My local computer runs PHP 5.3.2, while my server runs 5.2.5. I get Parse
This only happens on my local machine (Windows 7, Ruby 1.8.7). Occasionally rails will
Several co-workers and I are having a debate about what happens when a local
A quick question as to what happens to the local function while, if you
It usually happens to me that I make some local changes, only to discover
Something strange happens. I have in my code in a local function #ifdef abc
Many times it happens that I have few commits on my local Hg repository
Where are maven plugins stored? in the local repository as well? what happens if
I'm trying to install Ruby Bossman, and the following exception happens: /opt/local/lib/ruby/gems/1.8/gems/bossman-0.4.1/lib/bossman/boss.rb:10:in `initialize': undefined
What happens if I use SHGetFolderPath api call in a 32 bit system with

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.