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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:39:09+00:00 2026-06-15T04:39:09+00:00

I am looking for a simple way to do a task in background, then

  • 0

I am looking for a simple way to do a task in background, then update something (on the main thread) when it completes. It’s in a low level ‘model’ class so I can’t call InvokeOnMainThread as I don’t have an NSObject around. I have this method:

public void GetItemsAsync(Action<Item[]> handleResult)
{
  Item[] items=null;
  Task task=Task.Factory.StartNew(() =>
  {
    items=this.CreateItems(); // May take a second or two
  });
  task.ContinueWith(delegate
  {
    handleResult(items);
  }, TaskScheduler.FromCurrentSynchronizationContext());
}

This seems to work OK, but

1) Is this the best (simplest) way?

2) I’m worried about the local variable:

Item{} items=null

What stops that disappearing when the method returns before the background thread completes?

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-15T04:39:10+00:00Added an answer on June 15, 2026 at 4:39 am

    I think your method slightly violates a Single Responsibility Principle, because it doing too much.

    First of all I suggest to change CreateItems to return Task instead of wrapping it in the GetItemsAsync:

    public Task<Item[]> CreateItems(CancellationToken token)
    {
       return Task.Factory.StartNew(() => 
         // obtaining the data...
         {});
    }
    

    CancellationToken is optional but can help you if you able to cancel this long running operation.

    With this method you can remove GetItemsAsync entirely because its so simple to handle results by your client without passing this delegate:

    // Somewhere in the client of your class
    var task = yourClass.CreateItems(token);
    task.ContinueWith(t => 
     // Code of the delegate that previously
     // passed to GetItemsAsync method
     {}, TaskScheduler.FromCurrentSynchronizationContext());
    

    Using this approach you’ll get more clear code with only one responsibility. Task class itself is a perfect tool for representing asynchronous operation as a first class object. Using proposed technique you can easily mock you current implementation with a fake behavior for unit testing without changing your clients code.

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

Sidebar

Related Questions

I'm looking for a simple way of adding a refresh mechanism to my UIWebView.
I am looking for a simple way to make a piece of text, which
I'm looking for a simple way to translate long to String and back in
I'm looking for a simple way to have this done. I would to have
I am looking for a simple way (UDF?) to establish the similarity between strings.
I'm looking for as simple way to create an identity set. I just want
I am looking for a simple way to detect if a document has been
I'm looking for a simple way to tokenize std::string input without using non default
I'm looking for a simple/elegant way to grep a file such that every returned
I'm looking for a very simple way of determining if the version of Windows

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.