Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 如何处理普通任务的AggregateException(使用FSharpx.Core)?_F#_C# To F# - Fatal编程技术网

F# 如何处理普通任务的AggregateException(使用FSharpx.Core)?

F# 如何处理普通任务的AggregateException(使用FSharpx.Core)?,f#,c#-to-f#,F#,C# To F#,我相信我的问题是双重的。在下面,如何捕获aggregateeexception并在 任务是Task对象,而不是Task对象 当任务任务一个时(例如任务 下面是一个简短的、有点捏造的代码段。下面代码段中的taskGenerator模拟了一些外部C#library函数,该函数要么返回一个任务数组,要么返回一个与任务组合的任务。whalll 我不确定如何捕获生成的aggregateeexception并在泛型或非泛型情况下打印它。然后另一个问题是与非泛型异常的交互,我需要强制转换以满足FSharpx,

我相信我的问题是双重的。在下面,如何捕获
aggregateeexception
并在

  • 任务是
    Task
    对象,而不是
    Task
    对象
  • 当任务
    任务
    一个时(例如
    任务
  • 下面是一个简短的、有点捏造的代码段。下面代码段中的
    taskGenerator
    模拟了一些外部C#library函数,该函数要么返回一个
    任务数组
    ,要么返回一个与
    任务组合的
    任务。whalll

    我不确定如何捕获生成的
    aggregateeexception
    并在泛型或非泛型情况下打印它。然后另一个问题是与非泛型异常的交互,我需要强制转换以满足
    FSharpx
    ,但是
    忽略
    抛出可能不需要的异常。
    忽略
    函数来自,我相信这是一种相当常用的方法,可以将
    任务
    转换为
    任务

    打开FSharpx
    开放系统.Threading.Tasks
    开放系统
    打开FSharpx.Task
    []
    让主argv=
    让我们忽略(任务:任务)=
    让我们继续(t:任务):单位=
    如果t.出现故障,那么
    提出t.例外
    其他的
    ()
    task.ContinueWith continuation
    让taskGenerator()生成=
    让task=TaskBuilder(scheduler=TaskScheduler.Default)
    //下面只是模拟一个外部源,该源返回一组纯文本
    //任务。它还可以返回一个您希望等待的普通任务(例如。
    //此外部模拟源可以调用Task.WhenAll。)
    任务{
    让任务=
    [|1; 2; 3;|]
    |>序列图(趣味i->
    让taskSource=new TaskCompletionSource()
    SetException(新参数异常(“Argh!”)
    taskSource.Task:>Task)
    return!Task.WhenAll(tasks)|>忽略
    }
    让我们最后=
    让task=TaskBuilder(scheduler=TaskScheduler.Default)
    任务{
    设!a=taskGenerator()
    忽略
    }
    尝试
    让finalRes=final()
    最终|>忽略
    使用:?AggregateException作为aex->
    打印fn“%A”aex
    设x=System.Console.ReadLine()
    0
    
    我会使用
    任务。运行
    而不是捕获异常()事实上,在这种情况下,这看起来是一个合适的操作。你可以“传递”任务并将其带到某个地方。如果你能控制任务,也许最好“封装”使用try-catch-and-return
    选项的任务
    或其他一些域结构。我认为我感到的精神痛苦主要是由于普通的
    任务
    造成的。顺便说一句,很抱歉,我最近忙得不可开交。如果你愿意将你的评论作为答案推广,我可以接受。
    open FSharpx
    open System.Threading.Tasks
    open System
    open FSharpx.Task
    
    [<EntryPoint>]
    let main argv =  
        let Ignore(task:Task) =
            let continuation (t: Task): unit =
                if t.IsFaulted then
                    raise t.Exception
                else
                    ()
            task.ContinueWith continuation
    
        let taskGenerator() =
            let task = TaskBuilder(scheduler = TaskScheduler.Default)
            //The following just emulates an external source that returns a bunch of plain
            //tasks. It could return also one plain task one would like to await for (e.g.
            //this exernal, simulated source could call Task.WhenAll.)
            task {
                 let tasks =
                    [|1; 2; 3;|]
                    |> Seq.map(fun i ->
                        let taskSource = new TaskCompletionSource<unit>()
                        taskSource.SetException(new ArgumentException("Argh!"))
                        taskSource.Task :> Task)
                return! Task.WhenAll(tasks) |> Ignore
            }
    
        let final() =
            let task = TaskBuilder(scheduler = TaskScheduler.Default)
            task {
                let! a = taskGenerator()
                a |> ignore
            }
    
        try        
            let finalRes = final()
            finalRes |> ignore
        with :? AggregateException as aex ->
            printfn "%A" aex
    
    
        let x = System.Console.ReadLine()
    
        0