forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncResult.cs
More file actions
121 lines (99 loc) · 3.3 KB
/
AsyncResult.cs
File metadata and controls
121 lines (99 loc) · 3.3 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Threading;
using System.Web.Http;
namespace System.Net.Http.Internal
{
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "_manualResetEvent is disposed in End<TAsyncResult>")]
internal abstract class AsyncResult : IAsyncResult
{
private AsyncCallback _callback;
private object _state;
private bool _isCompleted;
private bool _completedSynchronously;
private bool _endCalled;
private Exception _exception;
protected AsyncResult(AsyncCallback callback, object state)
{
_callback = callback;
_state = state;
}
public object AsyncState
{
get { return _state; }
}
public WaitHandle AsyncWaitHandle
{
get
{
Contract.Assert(false, "AsyncWaitHandle is not supported -- use callbacks instead.");
return null;
}
}
public bool CompletedSynchronously
{
get { return _completedSynchronously; }
}
public bool HasCallback
{
get { return _callback != null; }
}
public bool IsCompleted
{
get { return _isCompleted; }
}
protected void Complete(bool completedSynchronously)
{
if (_isCompleted)
{
throw Error.InvalidOperation(Properties.Resources.AsyncResult_MultipleCompletes, GetType().Name);
}
_completedSynchronously = completedSynchronously;
_isCompleted = true;
if (_callback != null)
{
try
{
_callback(this);
}
catch (Exception e)
{
throw Error.InvalidOperation(e, Properties.Resources.AsyncResult_CallbackThrewException);
}
}
}
protected void Complete(bool completedSynchronously, Exception exception)
{
_exception = exception;
Complete(completedSynchronously);
}
protected static TAsyncResult End<TAsyncResult>(IAsyncResult result) where TAsyncResult : AsyncResult
{
if (result == null)
{
throw Error.ArgumentNull("result");
}
TAsyncResult thisPtr = result as TAsyncResult;
if (thisPtr == null)
{
throw Error.Argument("result", Properties.Resources.AsyncResult_ResultMismatch);
}
if (!thisPtr._isCompleted)
{
thisPtr.AsyncWaitHandle.WaitOne();
}
if (thisPtr._endCalled)
{
throw Error.InvalidOperation(Properties.Resources.AsyncResult_MultipleEnds);
}
thisPtr._endCalled = true;
if (thisPtr._exception != null)
{
throw thisPtr._exception;
}
return thisPtr;
}
}
}