请看下面的代码 -
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)
如果我从那里继续,程序不会崩溃并显示与发布模式相同的输出.这是处理异常的正确方法吗?
我正在开发一个遵循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) 我在 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) 假设我有一个类有两个string类型的属性
并且有以下限制
定义其值彼此依赖的属性的最佳方法是什么,以及在一个属性中进行的更改应该触发另一个属性的setter属性?
我是Angular2的新手并尝试从头开始开发应用程序.在该应用程序中,我需要广泛使用调度程序,网格,图表等控件以及其他控件.我一直在寻找谷歌,并遇到了Syncfusion,Kendo UI,DevExtreme等但无法找到决定性的解决方案.请建议我应该使用哪种Thrid方UI控件.我的基本需求是 -
性能
非常好的文档
来自特定公司以及使用相同公司的社区的轻松支持.
延迟加载
如果需要,可以在一定程度上轻松自定义控件.
请查看以下代码
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).请让我知道如何实现这一目标?如果可能,请提供示例代码.
c# ×4
.net ×3
task ×2
.net-4.0 ×1
angular ×1
dependencies ×1
devextreme ×1
exception ×1
kendo-ui ×1
modal-dialog ×1
mvvm ×1
properties ×1
syncfusion ×1
validation ×1
wpf ×1