小编Ani*_*aul的帖子

在任务中继续处理异常的正确方法

请看下面的代码 -

static void Main(string[] args)
{
    // Get the task.
    var task = Task.Factory.StartNew<int>(() => { return div(32, 0); });

    // For error handling.
    task.ContinueWith(t => { Console.WriteLine(t.Exception.Message); }, 
        TaskContinuationOptions.OnlyOnFaulted);

    // If it succeeded.
    task.ContinueWith(t => { Console.WriteLine(t.Result); }, 
        TaskContinuationOptions.OnlyOnRanToCompletion);
    Console.ReadKey();
    Console.WriteLine("Hello");
}

private static int div(int x, int y)
{
    if (y == 0)
    {
        throw new ArgumentException("y");
    }
    return x / y;
}
Run Code Online (Sandbox Code Playgroud)

如果我在发布模式下执行代码,输出为"发生了一个或多个错误",一旦我点击"Enter"键,"Hello"也会显示.如果我在调试模式下运行代码,输出与但是在IDE中调试时,当控件执行该行时,会出现IDE异常消息("用户代码中未处理的异常")

throw new ArgumentException("y"); 
Run Code Online (Sandbox Code Playgroud)

如果我从那里继续,程序不会崩溃并显示与发布模式相同的输出.这是处理异常的正确方法吗?

.net c# task task-parallel-library

52
推荐指数
4
解决办法
5万
查看次数

使用服务定位器的MVVM模态对话框

我正在开发一个遵循MVVM模式的WPF应用程序.要显示模态对话框,我试着按照以下文章建议的方式. http://www.codeproject.com/Articles/36745/Showing-Dialogs-When-Using-the-MVVM-Pattern?fid=1541292&fr=26#xx0xx

但是在这些文章中,我观察到,从MainWindowViewModel调用DialogService接口的ShowDialog方法.

我的申请中的情况略有不同.MainWindow.xaml包含一个用户控件,表示包含按钮Add的ChildView.MainWindowViewModel包含另一个ViewModel,表示与ChildView绑定的ChildVM.ChildVM包含AddCommand,当调用与AddCommand相对应的AddExecute方法时,我需要显示模态对话框.我怎么能做到这一点?

编辑代码

     private Window FindOwnerWindow(object viewModel)
            {
                    FrameworkElement view = null;

        // Windows and UserControls are registered as view.
        // So all the active windows and userControls are contained in views
        foreach (FrameworkElement viewIterator in views)
        {
            // Check whether the view is an Window
            // If the view is an window and dataContext of the window, matches
            // with the viewModel, then set view = viewIterator
            Window viewWindow = viewIterator as Window;
            if (null != viewWindow) …
Run Code Online (Sandbox Code Playgroud)

wpf modal-dialog mvvm

6
推荐指数
1
解决办法
4723
查看次数

ThrowUnobservedTaskExceptions 不起作用

我在 Winform 中创建了一个测试应用程序来学习任务(c#)中的异常处理。目标框架是4.0。以下是我的异常处理代码

        var task = Task.Factory.StartNew<DataTable>(() => getDataTable(Convert.ToInt32  (this.textBoxOptionVal.Text)));

        task.ContinueWith(t =>
        {
            this.dataGridViewData.DataSource = t.Result as DataTable;
            this.textBoxRetVal.Text = "Success" ;
        },
        CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());

        task.ContinueWith(t =>
        {
            // Update UI (and UI-related data) here: failed status.
            // t.Exception contains the occured exception.
            AggregateException aggregateException = t.Exception;
            aggregateException.Handle(exception => true);
            this.dataGridViewData.DataSource = null;
            this.textBoxRetVal.Text = "Exception Thrown";
        },
        CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
Run Code Online (Sandbox Code Playgroud)

这很好用。getDataTable如果方法抛出任何异常,则continuewith OnlyOnFaulted执行该代码块。但后来我注释掉了块中的所有语句ContinueWith OnlyOnfaulted,并添加了一个config包含以下内容的文件

<?xml version="1.0"?>
 <configuration>
  <runtime>
    <ThrowUnobservedTaskExceptions enabled="true"/>
  </runtime> …
Run Code Online (Sandbox Code Playgroud)

c# exception .net-4.0 task

5
推荐指数
1
解决办法
3285
查看次数

如何定义彼此依赖的属性

假设我有一个类有两个string类型的属性

  1. PROP1
  2. PROP2

并且有以下限制

  1. 如果Prop2值为"Test2",则Prop1不能等于"Test1"
  2. 如果Prop1值为"Test11",则Prop2不能等于"Test22"
  3. 如果Prop2 ="Test222",则设置Prop1 ="Test111"

定义其值彼此依赖的属性的最佳方法是什么,以及在一个属性中进行的更改应该触发另一个属性的setter属性?

.net c# validation dependencies properties

3
推荐指数
1
解决办法
1691
查看次数

Angular 2的最佳第三方UI控件

我是Angular2的新手并尝试从头开始开发应用程序.在该应用程序中,我需要广泛使用调度程序,网格,图表等控件以及其他控件.我一直在寻找谷歌,并遇到了Syncfusion,Kendo UI,DevExtreme等但无法找到决定性的解决方案.请建议我应该使用哪种Thrid方UI控件.我的基本需求是 -

  • 性能

  • 非常好的文档

  • 来自特定公司以及使用相同公司的社区的轻松支持.

  • 延迟加载

  • 如果需要,可以在一定程度上轻松自定义控件.

syncfusion kendo-ui devextreme angular

2
推荐指数
1
解决办法
2605
查看次数

单元测试调用另一个类的其他方法的方法

请查看以下代码

int sum(int a, int b)
{
    int x = memberInstance.xyz(a); // memberInstance is an object of another class
    .....
    .....
}
Run Code Online (Sandbox Code Playgroud)

比如,还知道xyz方法返回1-10之间的数字.现在,我想为sum方法开发单元测试方法,我想用任意返回值[1-10之间的任何值] 替换方法调用memberInstance.xyz(a).请让我知道如何实现这一目标?如果可能,请提供示例代码.

.net c# visual-studio-2010

1
推荐指数
1
解决办法
1740
查看次数