0

Imagine there is a method

async static System.Threading.Tasks.Task Do()
{
    //do sth
    await Task.Delay(10000);
}

now , when I call the method with

Do();

Does it make a new thread ? or I have to create a thread as below

Task.Factory.StartNew(() => Do());
2
  • You have to use the await keyword, just like you did with Task.Delay. Although the second method would work too, but then you wouldn't need the async modifier. Commented Oct 25, 2015 at 12:54
  • 1
    This article might be helpful blog.stephencleary.com/2013/11/there-is-no-thread.html Commented Oct 25, 2015 at 12:54

2 Answers 2

3

Do doesn't create a thread.

The part before the await runs on the calling thread and there's no thread at all during the delay. Task.Delay uses a timer internally which doesn't hold up a thread throughout the operation.

When the delay completes a thread from the ThreadPool is used to continue executing the method.

About Task.Factory.StartNew, why would you want to create a thread? If you need to offload a CPU intensive method to the thread pool then it's fine (although Task.Run is preferable) but if you don't then simply call Do and await the result:

await Do();

If you insist on creating a new thread for Do you need to use TaskCreationOptions.LongRunning:

Task.Factory.StartNew(() => Do(), TaskCreationOptions.LongRunning);

But since Do is an async method that releases the thread when it reaches the first await this doesn't make any sense as I've explained on my blog: LongRunning Is Useless For Task.Run With async-await

Sign up to request clarification or add additional context in comments.

3 Comments

so , if I call it like System.Threading.Tasks.Task.Factory.StartNew(() => Do()); , a new thread would be created ? if no, what is the solution to create a new thread before await ?
StartNew doesn't create a thread. It offloads to the ThreadPool. If you want to create a new thread you need to pass TaskCreationOptions.LongRunning but since your method is asynchronous that is pointless. You can read about it on my blog: LongRunning Is Useless For Task.Run With async-await
I want to have some threads that every thread does something different .
-1

All my case is this :

 public void Start(Action action)
    {
        Token = CancellationToken.None;
        IsRunning = true;

       Task.Factory.StartNew(() => Do(action), TaskCreationOptions.LongRunning);
    }

    async Task Do(Action action)
    {
        while (IsRunning)
        {
            action();

            await Task.Delay(Interval, Token);

        }
    }

then I call Start() with five different actions with different intervals. Is this Correct to use ?

7 Comments

As I stated in my answer, yes it is. Even though the LongRunning is redundant.
you said "But since Do is an async method that releases the thread when it reaches the first await" . does it release the thread when it reaches the first await here ? If so , How can I make it a thread which is alive until app runs?
You can't. The whole point of an async method is that the thread is released when it's not needed. That's not a drawback. It's a good thing.
but I want a thread that checks a queue , if there is any item in it, then dequeue and do some operations .it should be running all the time.
what about using a Thread.Sleep(Interval); instead of await Task.Delay(Interval, Token); ? or maybe I shouldn't use Task in my scenario ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.