可能最常见和最需要的编程模式是调用异步函数并且调用函数等待其完成。
Psuedo代码:
do
Execute TimeConsumingSub asynchronously
Wait for TimeConsumingSub to complete for a maximum of 100 seconds
if TimeConsumingSub has timed out
do something
else
do something else
end if
loop
这个伪代码的c#,vb.net等价是什么?
答案 0 :(得分:0)
以下代码是等效的,尽可能小:
Dim _TimeConsumingSubCompleted as Boolean = False
'global variable to use, set to true at end of your subroutine.
Do
Await Threading.Tasks.Task.WaitAny(TimeConsumingSub, Threading.Tasks.Task.Delay(100000))
If _TimeConsumingSubCompleted Then
'do something
Else
'Do something else
End If
Loop
这使用Task.WaitAny来运行你的消费子和彼此相邻的延迟,并完成等待执行,直到任何一个完成。