Asynchronously wait for Task to complete with timeout

后端 未结 16 2018
天命终不由人
天命终不由人 2020-11-21 17:49

I want to wait for a Task to complete with some special rules: If it hasn\'t completed after X milliseconds, I want to display a message to the user. And

16条回答
  •  时光说笑
    2020-11-21 18:37

    You can use Task.WaitAny to wait the first of multiple tasks.

    You could create two additional tasks (that complete after the specified timeouts) and then use WaitAny to wait for whichever completes first. If the task that completed first is your "work" task, then you're done. If the task that completed first is a timeout task, then you can react to the timeout (e.g. request cancellation).

提交回复
热议问题