Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4054701

Browse files
OnurGumusKevinRansom
authored andcommitted
[RFC FS-1028] - Implement Async.StartImmediateAsTask (dotnet#2534)
Thank you for this contributionKevin
1 parent7228139 commit4054701

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Control/AsyncType.fs‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,102 @@ type AsyncType() =
275275
Assert.IsTrue(t.IsCanceled)
276276
Assert.IsTrue(!cancelled)
277277

278+
[<Test>]
279+
memberthis.CreateImmediateAsTask()=
280+
lets="Hello tasks!"
281+
leta=async{return s}
282+
#if FSCORE_PORTABLE_NEW|| coreclr
283+
lett:Task<string>=
284+
#else
285+
use t:Task<string>=
286+
#endif
287+
Async.StartImmediateAsTask a
288+
this.WaitASec t
289+
Assert.IsTrue(t.IsCompleted)
290+
Assert.AreEqual(s, t.Result)
291+
292+
[<Test>]
293+
memberthis.StartImmediateAsTask()=
294+
lets="Hello tasks!"
295+
leta=async{return s}
296+
#if FSCORE_PORTABLE_NEW|| coreclr
297+
lett=
298+
#else
299+
use t=
300+
#endif
301+
Async.StartImmediateAsTask a
302+
this.WaitASec t
303+
Assert.IsTrue(t.IsCompleted)
304+
Assert.AreEqual(s, t.Result)
305+
306+
307+
[<Test>]
308+
memberthis.ExceptionPropagatesToImmediateTask()=
309+
leta=async{
310+
do raise(Exception())
311+
}
312+
#if FSCORE_PORTABLE_NEW|| coreclr
313+
lett=
314+
#else
315+
use t=
316+
#endif
317+
Async.StartImmediateAsTask a
318+
let mutableexceptionThrown=false
319+
try
320+
this.WaitASec t
321+
with
322+
e-> exceptionThrown<-true
323+
Assert.IsTrue(t.IsFaulted)
324+
Assert.IsTrue(exceptionThrown)
325+
326+
[<Test>]
327+
memberthis.CancellationPropagatesToImmediateTask()=
328+
leta=async{
329+
whiletruedo()
330+
}
331+
#if FSCORE_PORTABLE_NEW|| coreclr
332+
lett=
333+
#else
334+
use t=
335+
#endif
336+
Async.StartImmediateAsTask a
337+
Async.CancelDefaultToken()
338+
let mutableexceptionThrown=false
339+
try
340+
this.WaitASec t
341+
with e-> exceptionThrown<-true
342+
Assert.IsTrue(exceptionThrown)
343+
Assert.IsTrue(t.IsCanceled)
344+
345+
[<Test>]
346+
memberthis.CancellationPropagatesToGroupImmediate()=
347+
letewh=new ManualResetEvent(false)
348+
letcancelled= reffalse
349+
leta=async{
350+
use! holder= Async.OnCancel(fun _-> cancelled:= true)
351+
ewh.Set()|> Assert.IsTrue
352+
whiletruedo()
353+
}
354+
letcts=new CancellationTokenSource()
355+
lettoken= cts.Token
356+
#if FSCORE_PORTABLE_NEW|| coreclr
357+
lett=
358+
#else
359+
use t=
360+
#endif
361+
Async.StartImmediateAsTask(a, cancellationToken=token)
362+
// printfn "%A" t.Status
363+
ewh.WaitOne()|> Assert.IsTrue
364+
cts.Cancel()
365+
// printfn "%A" t.Status
366+
let mutableexceptionThrown=false
367+
try
368+
this.WaitASec t
369+
with e-> exceptionThrown<-true
370+
Assert.IsTrue(exceptionThrown)
371+
Assert.IsTrue(t.IsCanceled)
372+
Assert.IsTrue(!cancelled)
373+
278374

279375
[<Test>]
280376
memberthis.TaskAsyncValue()=

‎src/fsharp/FSharp.Core/control.fs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,17 @@ namespace Microsoft.FSharp.Control
12181218
static memberStartWithContinuations(computation:Async<'T>,continuation,exceptionContinuation,cancellationContinuation,?cancellationToken):unit=
12191219
Async.StartWithContinuationsUsingDispatchInfo(computation, continuation,(fun edi-> exceptionContinuation(edi.GetAssociatedSourceException())), cancellationContinuation, ?cancellationToken=cancellationToken)
12201220

1221+
static memberStartImmediateAsTask(computation:Async<'T>,?cancellationToken):Task<'T>=
1222+
lettoken= defaultArg cancellationToken defaultCancellationTokenSource.Token
1223+
letts=new TaskCompletionSource<'T>()
1224+
lettask= ts.Task
1225+
Async.StartWithContinuations(
1226+
computation,
1227+
(fun(k)-> ts.SetResult(k)),
1228+
(fun exn-> ts.SetException(exn)),
1229+
(fun _-> ts.SetCanceled()),
1230+
token)
1231+
task
12211232
static memberStartImmediate(computation:Async<unit>,?cancellationToken):unit=
12221233
lettoken= defaultArg cancellationToken defaultCancellationTokenSource.Token
12231234
CancellationTokenOps.StartWithContinuations(token, computation, id,(fun edi-> edi.ThrowAny()), ignore)

‎src/fsharp/FSharp.Core/control.fsi‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,23 @@ namespace Microsoft.FSharp.Control
415415
computation:Async<unit>* ?cancellationToken:CancellationToken-> unit
416416

417417

418+
///<summary>Runs an asynchronous computation, starting immediately on the current operating system,
419+
/// but also returns the execution as<c>System.Threading.Tasks.Task</c>
420+
///</summary>
421+
///<remarks>If no cancellation token is provided then the default cancellation token is used.
422+
/// You may prefer using this method if you want to achive a similar behviour to async await in C# as
423+
/// async computation starts on the current thread with an ability to return a result.
424+
///</remarks>
425+
///<param name="computation">The asynchronous computationto execute.</param>
426+
/// <param name="cancellationToken">The <c>CancellationToken</c> to associate with the computation.
427+
/// The default is used if this parameter is not provided.</param>
428+
/// <returns>A <c>System.Threading.Tasks.Task</c> that will be completed
429+
/// in the corresponding state once the computation terminates (produces the result, throws exception or gets canceled)</returns>
430+
/// </returns>
431+
static memberStartImmediateAsTask:
432+
computation:Async<'T>* ?cancellationToken:CancellationToken-> Task<'T>
433+
434+
418435

419436
[<CompiledName("FSharpAsyncBuilder")>]
420437
[<Sealed>]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp