【问题标题】:C# and Caliburn - RescueAttribute and CoroutinesC# 和 Caliburn - RescueAttribute 和协程
【发布时间】:2011-05-08 02:51:24
【问题描述】:

我想我发现了 RescueAttribute 被破坏的情况。或者我可能不正确地使用协程。

我有一个这样的 ViewModel:

[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 中取消注释异常,Rescue 将无法处理异常。

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

如果您在 yield 之前放置一个异常以返回 AsyncResult,Rescue 就可以正常工作。此外,如果在异步线程抛出异常,救援仍然有效!

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

【问题讨论】:

    标签: c# .net mvvm caliburn coroutine


    【解决方案1】:

    这似乎是一个合法的错误。我在 Caliburn 跟踪器中为此添加了一个问题:http://caliburn.codeplex.com/workitem/7636

    编辑:问题已解决
    见:http://caliburn.codeplex.com/Thread/View.aspx?ThreadId=234229

    【讨论】:

    • 谢谢。我们花了一些时间来追踪这个问题,直到我可以制作一个简单的例子来重现。
    【解决方案2】:

    我提议对 IDispatcher 进行更改以解决此问题:

    http://caliburn.codeplex.com/Thread/View.aspx?ThreadId=223873

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2017-05-15
      • 2022-10-14
      • 2012-01-29
      • 1970-01-01
      相关资源
      最近更新 更多