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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:21:53+00:00 2026-06-17T21:21:53+00:00

OK first of all, it’s nothing that I need to implement or anything. I

  • 0

OK first of all, it’s nothing that I need to implement or anything. I just need to know the answer because someone more experienced told me that asynchronous execution doesn’t necessarily have to involve a new thread as threads are somewhat heavy constructs, which confused me a lot and I couldn’t agree.

Now let’s say, I have two methods – Execute() and ExecuteAsync(). Execute() is running on the main thread. I want to call ExecuteAsync() from within Execute() and I don’t care whenever it completes executing, but when it does, may be (or may be not) I want use it’s return value. That’s a typical example of an asynchronous execution, right?

I know I can do this using BackgroundWorker or IAsyncResult (Delegate.BeginInvoke()), but AFAIK under the hood they spawns a secondary CLR Thread/ThreadPool Thread.

So is it anyhow possible to execute the method ExecuteAsync() asynchronously without the help of a second thread?

EDIT : I think this edit will clarify the scenario further. Invoking ExecuteAsync() is NOT the only (or last) task for Execute() to perform. Execute() should continue it’s own tasks without caring about the execution of ExecuteAsync() method.

  • 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-17T21:21:55+00:00Added an answer on June 17, 2026 at 9:21 pm

    Here is an example of a program that uses asynchrony and never ever uses more than one thread:

    public class Foo
    {
        private int _value;
        private TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                var oldTCS = tcs;
                tcs = new TaskCompletionSource<bool>();
                oldTCS.SetResult(true);
            }
        }
    
    
        public Task ValueChanged()
        {
            return tcs.Task;
        }
    }
    
    private static void Main(string[] args)
    {
        Foo foo = new Foo();
        foo.ValueChanged()
            .ContinueWith(t =>
            {
                Console.WriteLine(foo.Value);
            }, TaskContinuationOptions.ExecuteSynchronously);
    
        foo.Value = 5;
    }
    

    The Task returned from ValueChanged will be completed the next time that Value is changed. The user of the Foo class can get that returned task and wire up continuations to run on that task based on an operation that has not yet happened. Then, at some point in the future, the value of foo is changed, and the continuation will run. Note that the foo object could be passed to some other function, entirely unknown to Main, that ends up setting the value (to show why you might want to do something like this).

    No new thread is needed to create the Task, nor to execute the continuation.

    Here’s another example that’s much more practical:

    We’ll start with this simple (extension) method that takes a form and returns a Task indicating when that form is next closed:

    public static class FormExtensions
    {
        public static Task WhenClosed(this Form form)
        {
            var tcs = new TaskCompletionSource<bool>();
            form.FormClosed += (sender, args) => tcs.SetResult(true);
            return tcs.Task;
        }
    }
    

    Now we can have this in one of our forms:

    private async void button1_Click(object sender, EventArgs args)
    {
        Form2 otherForm = new Form2();
        otherForm.Show();
    
        await otherForm.WhenClosed();
    
        //take some data from that form and display it on this form:
        textBox1.Text = otherForm.Name;
    }
    

    Creating and showing another form never involves the creation of new threads. Both this form and the new form use entirely the one UI thread to be created and modified.

    The creation of the Task returned from WhenClosed does not need to create a new thread at all.

    When the Task is awaited, no new thread is created. The current method ends and the UI thread is left to go back to processing messages. At some point, that same UI thread will do something that results in the second form being closed. That will result in the continuation of the task running, thus returning us to our button click handler where we set the text of the textbox.

    All of this is done entirely with the UI thread, no other threads have been created. And yet we’ve just “waited” (without actually waiting) for a long running operation to finish (the user to put some information into the second form and then close it) without blocking the UI thread, thus keeping the main form responsive.

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

Sidebar

Related Questions

first of all i would like to say i know its probably an easy
First of all, apologize because I have seen some posts about this, but I
First of all, let me say that I am not a professional coder -
First of all I don't know If controller is the right word. What I
First of all, let me state that I am very new to Bash scripting.
First of all I have to admit that these are very basic and primitive
First of all, I'm using ARC. If I have a class that, for example,
First of all, pardon my hacky code, I'm just trying to try this out
first of all, I'm not a programmer, so the answer to this might be
First of all I have seen that there are many questions about unrecognized selector

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.