This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathasyncworkerexceptionhandling.html
73 lines (63 loc) · 2.26 KB
/
asyncworkerexceptionhandling.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
layout: documentation
title: EvaluationEngine
teaser: Decouple business logic from control flow
navigation:
- name: Overview
link: asyncworker.html
- name: Execute asynchronous operation
link: asyncworkerasyncoperation.html
- name: Execute code on completion
link: asyncworkercompletion.html
- name: Exception Handling
link: asyncworkerexceptionhandling.html
- name: Cancel asynchronous operation
link: asyncworkercancel.html
- name: Use progress
link: asyncworkerprogress.html
- name: Testability
link: asyncworkertestability.html
---
<h2>Exception Handling</h2>
<p>
If there was an exception thrown by the worker delegate, the exception is passed to the completion callback.
</p>
<p>
The completion callback will be called on the thread on which the async worker was started if this thread supports synchronization (UI thread do support synchronization).
Otherwise, it will be called on the worker thread.
</p>
<h3>Failing Operation with no Completion Handler</h3>
<p>
If the operation throws an exception and there is no completion handler set then the global handler for unhandled exceptions is notified:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
// first we need to register for unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
// handle the unhandled exception
};
DoWorkEventHandler worker = delegate
{
throw new InvalidOperationException("test");
};
AsyncWorker asyncWorker = new AsyncWorker(worker);
asyncWorker.RunWorkerAsync();
]]></script>
<h3>Failing Operation with Completion Handler</h3>
<p>
If the operation throws an exception and there is a completion handler set then the completion handler is responsible to handle the exception. The global handler for unhandled exceptions will not be notified:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
DoWorkEventHandler worker = delegate
{
throw new InvalidOperationException();
};
RunWorkerCompletedEventHandler completed =
delegate(object sender, RunWorkerCompletedEventArgs e)
{
// handle the exception (null if no exception)
Exception exception = e.Error;
};
AsyncWorker asyncWorker= new AsyncWorker(worker, completed);
asyncWorker.RunWorkerAsync();
]]></script>