简体   繁体   中英

How to block the current task only, without blocking the other tasks on the same thread?

Is there a way in C# to wait until a specific task returns or timeout after a specified number of milliseconds, withOUT making all the other tasks running on the same thread be blocked as well?

Assuming you are starting the tasks individually, and not using Parallel.For/ForEach/Invoke, etc. ie You are getting a Task object back, then something like this:

Task taskIWantToWaitFor = Task.Factory.Start(....);
// Other code
taskIWantToWaitFor.Wait(millisecondsTimeout)
// All other tasks continue in the background

I reference the answer of @Dorus on this topic:

TL;DR:

Task delay = Task.Delay(5000);
await delay;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM