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
Exception 如何在F中引用c'tor中的当前实例?_Exception_F#_Actor - Fatal编程技术网

Exception 如何在F中引用c'tor中的当前实例?

Exception 如何在F中引用c'tor中的当前实例?,exception,f#,actor,Exception,F#,Actor,下面用黑体字标出其中3个相关问题 我试图创建一个F类-我需要使用它,而不引用C中F的程序集 我有这样的代码: type ReceiverExceptionEventArgs(ex:Exception) = inherit EventArgs() member this.Exception = ex type ExceptionRaised = delegate of obj * ReceiverExceptionEventArgs -> unit type Receiv

下面用黑体字标出其中3个相关问题

我试图创建一个F类-我需要使用它,而不引用C中F的程序集

我有这样的代码:

type ReceiverExceptionEventArgs(ex:Exception) =
    inherit EventArgs()
    member this.Exception = ex

type ExceptionRaised = delegate of obj * ReceiverExceptionEventArgs -> unit

type Receiver( ... ) =

  let error  = new Event<ExceptionRaised, ReceiverExceptionEventArgs>()

  let a = new Actor<...>(fun inbox -> ...)   

  [<CLIEvent>]
  member __.Error = error.Publish

  /// Starts the receiver which starts the consuming from the service bus
  /// and creates the queue if it doesn't exist
  member x.Start () =
    logger.InfoFormat("start called for queue '{0}'", desc)
    a.Post Start
但我在上面一行没有提到“这个”。。。我将如何用F来惯用它

更复杂的是,参与者开始了大量基本上是循环的异步计算;这些计算返回单位,所以我不能只做:

let startPairsAsync pairs token =
  async {
    for Pair(mf, rs) in pairs do
      for r in rs do 
        match Async.Start(Async.Catch(r |> worker), token) with
        | Choice1Of2 () -> ()
        | Choice2Of2 ex -> error.Trigger(null, new ReceiverExceptionEventArgs(ex)) }
因为worker r是异步的,所以我得到这个表达式的类型应该是'unit',但是在第一个模式匹配中这里是'Choice1of2'

离题:

我为每个员工创建演员的想法也是这样吗?我希望有类似于erlang的“管理器”的代码,因为我正在使用的代码几乎在任何网络点都可能任意失败。。。监事不在本问题范围内;但是,我是否可以用多个参与者而不是Async.Catch更好地处理惯用异常呢?

关于这一点,请使用

在声明中,将其绑定到构造函数中。您可以使用任何您喜欢的标识符

另见

关于这一点,请使用

在声明中,将其绑定到构造函数中。您可以使用任何您喜欢的标识符

另见


关于第二个涉及异步工作流的问题,我不完全理解您的代码,但是如果您试图启动一些异步计算并处理执行时可能发生的异常,那么使用try会容易得多。。。使用内部异步工作流而不是Async.Catch。这样可能更好:

let startPairsAsync pairs token = 
  let work = 
    [ for Pair(mf, rs) in pairs do 
        for r in rs do  
          // Produce asynchronous computation that is 
          // protected using a try .. with block
          yield async { 
            try do! worker r 
            with e -> error.Trigger(null, new ReceiverExceptionEventArgs(ex)) } ]
    // Compose them to run all computations in parallel
    |> Async.Parallel

  // Run composed computation with a cancellation token
  Async.Start(work, token)

关于涉及异步工作流的第二个问题,我不完全理解您的代码,但是如果您尝试启动一些异步计算并处理执行时可能发生的异常,那么使用try会容易得多。。。使用内部异步工作流而不是Async.Catch。这样可能更好:

let startPairsAsync pairs token = 
  let work = 
    [ for Pair(mf, rs) in pairs do 
        for r in rs do  
          // Produce asynchronous computation that is 
          // protected using a try .. with block
          yield async { 
            try do! worker r 
            with e -> error.Trigger(null, new ReceiverExceptionEventArgs(ex)) } ]
    // Compose them to run all computations in parallel
    |> Async.Parallel

  // Run composed computation with a cancellation token
  Async.Start(work, token)

一般来说您是否建议对参与者使用标准异常处理?难道没有更粗粒度的方法吗?@Henrik-我不知道你指的演员是什么意思?您是指F MailboxProcessor类型,也称为代理还是异步工作流?通常,使用try。。使用in-async{..}块应该可以满足您的所有需要。请注意,异常会自动传播,因此如果将整个计算包装在一个处理程序中,这不会太细粒度;您是否建议对参与者使用标准异常处理?难道没有更粗粒度的方法吗?@Henrik-我不知道你指的演员是什么意思?您是指F MailboxProcessor类型,也称为代理还是异步工作流?通常,使用try。。使用in-async{..}块应该可以满足您的所有需要。请注意,异常会自动传播,因此如果将整个计算包装在一个处理程序中,这不会太细粒度。您好,是的,我是说MailboxProcessor-我可以像与actors一样将它们链接在一起吗?
let startPairsAsync pairs token = 
  let work = 
    [ for Pair(mf, rs) in pairs do 
        for r in rs do  
          // Produce asynchronous computation that is 
          // protected using a try .. with block
          yield async { 
            try do! worker r 
            with e -> error.Trigger(null, new ReceiverExceptionEventArgs(ex)) } ]
    // Compose them to run all computations in parallel
    |> Async.Parallel

  // Run composed computation with a cancellation token
  Async.Start(work, token)