2010-11-10 64 views
4

我认为我发现RescueAttribute被破坏的情况。或者,也许我错误地使用了co-routines。C#和Caliburn - RescueAttribute和协程

我有一个这样的视图模型:

[Rescue("Rescue")] 
class MyViewModel 
{ 
    //... left out some bus-logic code here ... 

    public void Login() 
    { 
     yield return Show.Busy(); 

     //the following line will also cause the problem, just like AsyncResult 
     //yield return Show.MessageBox("Test"); 

     yield return new AsyncResult(() => _server.Login()); 

     //throw new Exception("Aww, snap!"); 

     yield return Show.NotBusy(); 
    } 

    public void Rescue(Exception exc) 
    { 
     //Show a messagebox or something 
    } 
} 

AsyncResult是像这样实现的:

using Exec = Caliburn.PresentationFramework.Invocation.Execute; 

    public class AsyncResult : IResult 
    { 
     private readonly Action _function; 

     public AsyncResult(Action function) 
     { 
      _function = function; 
     } 

     public void Execute(ResultExecutionContext context) 
     { 
      Exec.OnBackgroundThread(delegate 
      { 
       try 
       { 
        _function(); 
       } 
       catch (Exception exc) 
       { 
        Exec.OnUIThread(() => Completed(this, new ResultCompletionEventArgs { Error = exc, WasCancelled = true })); 
        return; 
       } 
       Exec.OnUIThread(() => Completed(this, new ResultCompletionEventArgs())); 
      }); 
     } 

     public event EventHandler<ResultCompletionEventArgs> Completed = delegate { }; 
    } 

如果我去掉异常在我上面的ViewModel,营救无法处理异常。

这是Caliburn中的错误,还是AsyncResult实现错误?

如果您在yield返回AsyncResult之前放置一个异常,Rescue工作得很好。另外如果抛出异常上的异步线程,救援仍然有效!

编辑:你也可以使用Show.MessageBox而不是AsyncResult来重现相同的问题。

回答