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

The Archive Base Latest Questions

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

As you might know, Android’s SDK features a AsyncTask class which allows for code

  • 0

As you might know, Android’s SDK features a AsyncTask class which allows for code execution on a separate thread and result acquisition in the main (UI) thread. In short, something like this:

class AsyncTask
{
   void onPreExecute(...)
   {
       //Executed on the main thread BEFORE the secondary thread gets to work on anything.
   }

   void execute(...)
   {
       //This runs on the secondary thread.
   }

   void onPostExecute(...)
   {
       //This runs on the main thread AFTER the secondary thread finishes work. You get the result here in the form of some data type.
   }
}

Of course, this is just a rough scheme, but it should provide enough info on the AsyncTask if you’re not familiar with it. Basically, I’m looking for the same functionality in Microsoft’s .NET Framework.

Before I start working on my own classes to do this, I want to be sure there’s nothing that could allow me to get the desired result already in the framework. I’m using .NET 4.0. Perhaps some sort of ‘clever’ use of System.Threading.Tasks.Task? Have no idea, I’ll leave this to you.

In short, I want to pass some input to a function, run the code on a secondary thread, and on completion, update some UI elements via the main thread. Locking the main thread (via Task.Wait() for example) doesn’t work well with my requirements.

  • 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-25T14:18:55+00:00Added an answer on May 25, 2026 at 2:18 pm

    You can achieve what you’re looking for using the Task class in .Net.

    Here’s what some code looks like that should help get you started:

    var task = Task.Factory.StartNew(() => YourMethodGoesHere());
    
    task.ContinueWith(t => UpdateYourUiInThisContinuation(),
                      TaskScheduler.FromCurrentSynchronizationContext());
    
    task.ContinueWith(t => HandleAnExceptionWhichTheTaskMayThrow(), 
                       TaskContinuationOptions.OnlyOnFaulted);
    

    That will schedule the YourMethodGoesHere() to run off the UI thread. The continuation, UpdateYourUiInThisContinuation() will be scheduled to run once the initial task completes, and the overload I used will force it to continue within the same synchronization context (UI thread, assuming this code is being called on the UI thread initially).

    The last continuation is good practice to handle any exceptions which code within the task may thrown. If you don’t handle it (there are other ways other than using this continuation), you’ll end up with an unhandled AggregateException.

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

Sidebar

Related Questions

In an Android application I run a task in a separate thread. While the
Does anyone know of a good way to have code run after their Android
To recap for those .NET gurus who might not know the Java API: ConcurrentHashMap
I know this might be a no-brainer, but please read on. I also know
I know it might not be according to Apple's human interface guidelines for the
I know this question might sound a little cheesy but this is the first
I might not like it, but the number of unofficial Android devices is growing
Is there an Image cropping Activity in Android? I know that when you save
I'm using Android 2.2, which comes with a version of STLport. For some reason,
I want to read (only) records from data/data/com.android.providers.telephony/databases/mmssms.db (I know no official API, etc.

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.