@@ -104,19 +104,19 @@ module internal Utils =
104104
105105 static member internal chooseTasks ( a : Task < 'T >) ( b : Task < 'U >) : Async < Choice < 'T * Task < 'U >, 'U * Task < 'T >>> =
106106 async {
107- let! ct = Async.CancellationToken
108- let i = Task.WaitAny ( [| ( a :> Task );( b :> Task ) |], ct )
109- if i = 0 then return ( Choice1Of2 ( a.Result, b))
110- elif i = 1 then return ( Choice2Of2 ( b.Result, a))
111- else return ! failwith ( sprintf " unreachable, i = %d " i ) }
107+ let ta , tb = a :> Task , b :> Task
108+ let! i = Task.WhenAny ( ta , tb ) |> Async.AwaitTask
109+ if i = ta then return ( Choice1Of2 ( a.Result, b))
110+ elif i = tb then return ( Choice2Of2 ( b.Result, a))
111+ else return ! failwith " unreachable" }
112112
113113 static member internal chooseTasks2 ( a : Task < 'T >) ( b : Task ) : Async < Choice < 'T * Task , Task < 'T >>> =
114114 async {
115- let! ct = Async.CancellationToken
116- let i = Task.WaitAny ( [| ( a :> Task );( b ) |], ct )
117- if i = 0 then return ( Choice1Of2 ( a.Result, b))
118- elif i = 1 then return ( Choice2Of2 ( a))
119- else return ! failwith ( sprintf " unreachable, i = %d " i ) }
115+ let ta = a :> Task
116+ let! i = Task.WhenAny ( ta , b ) |> Async.AwaitTask
117+ if i = ta then return ( Choice1Of2 ( a.Result, b))
118+ elif i = b then return ( Choice2Of2 ( a))
119+ else return ! failwith " unreachable" }
120120
121121 type MailboxProcessor < 'Msg > with
122122 member __.PostAndAsyncReplyTask ( f : TaskCompletionSource < 'a > -> 'Msg ) : Task < 'a > =
@@ -1493,20 +1493,20 @@ module AsyncSeq =
14931493 let tasks = Array.zeroCreate n
14941494 for i in 0 .. ss.Length - 1 do
14951495 let! task = Async.StartChildAsTask ( ies.[ i]. MoveNext())
1496- do tasks.[ i] <- ( task :> Task )
1496+ do tasks.[ i] <- task
14971497 let fin = ref n
14981498 while fin.Value > 0 do
1499- let! ct = Async.CancellationToken
1500- let i = Task.WaitAny ( tasks, ct )
1501- let v = ( tasks .[ i ] :?> Task < 'T option >) .Result
1499+ let! ti = Task.WhenAny ( tasks ) |> Async.AwaitTask
1500+ let i = Array.IndexOf ( tasks, ti )
1501+ let v = ti .Result
15021502 match v with
15031503 | Some res ->
15041504 yield res
15051505 let! task = Async.StartChildAsTask ( ies.[ i]. MoveNext())
1506- do tasks.[ i] <- ( task :> Task )
1507- | None ->
1506+ do tasks.[ i] <- task
1507+ | None ->
15081508 let t = System.Threading.Tasks.TaskCompletionSource()
1509- tasks.[ i] <- ( t.Task :> Task ) // result never gets set
1509+ tasks.[ i] <- t.Task // result never gets set
15101510 fin := fin .Value - 1
15111511 }
15121512
0 commit comments