Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
C# 中是否有办法在指定的毫秒数后等待特定任务返回或超时,而不会阻止在同一线程上运行的所有其他任务?
假设您正在单独启动任务,而不是使用 Parallel.For/ForEach/Invoke 等,即您正在获取一个 Task 对象,然后是这样的:
Task taskIWantToWaitFor = Task.Factory.Start(....); // Other code taskIWantToWaitFor.Wait(millisecondsTimeout) // All other tasks continue in the background
我参考了@Dorus关于这个主题的答案:
TL;博士:
Task delay = Task.Delay(5000); await delay;