Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / xsp / System / Web / AspNetSynchronizationContext.cs / 1 / AspNetSynchronizationContext.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Threading;
using System.Web;
using System.Web.Util;
namespace System.Web {
internal class AspNetSynchronizationContext : SynchronizationContext {
private HttpApplication _application;
private bool _disabled;
private bool _syncCaller;
private bool _invalidOperationEncountered;
private int _pendingCount;
private Exception _error;
private WaitCallback _lastCompletionWorkItemCallback;
internal AspNetSynchronizationContext(HttpApplication app) {
_application = app;
}
private void CallCallback(SendOrPostCallback callback, Object state) {
// don't take app lock for [....] caller to avoid deadlocks in case they poll for result
if (_syncCaller) {
CallCallbackPossiblyUnderLock(callback, state);
}
else {
lock (_application) {
CallCallbackPossiblyUnderLock(callback, state);
}
}
}
private void CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) {
HttpApplication.ThreadContext threadContext = null;
try {
threadContext = _application.OnThreadEnter();
try {
callback(state);
}
catch (Exception e) {
_error = e;
}
}
finally {
if (threadContext != null) {
threadContext.Leave();
}
}
}
internal int PendingOperationsCount {
get { return _pendingCount; }
}
internal Exception Error {
get { return _error; }
}
internal void ClearError() {
_error = null;
}
internal void SetLastCompletionWorkItem(WaitCallback callback) {
Debug.Assert(_lastCompletionWorkItemCallback == null); // only one at a time
_lastCompletionWorkItemCallback = callback;
}
public override void Send(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Send");
Debug.Trace("AsyncStack", "Send from:\r\n" + System.Environment.StackTrace);
#endif
CallCallback(callback, state);
}
public override void Post(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Post");
Debug.Trace("AsyncStack", "Post from:\r\n" + System.Environment.StackTrace);
#endif
CallCallback(callback, state);
}
#if DBG
[EnvironmentPermission(SecurityAction.Assert, Unrestricted=true)]
private void CreateCopyDumpStack() {
Debug.Trace("Async", "CreateCopy");
Debug.Trace("AsyncStack", "CreateCopy from:\r\n" + System.Environment.StackTrace);
}
#endif
public override SynchronizationContext CreateCopy() {
#if DBG
CreateCopyDumpStack();
#endif
AspNetSynchronizationContext context = new AspNetSynchronizationContext(_application);
context._disabled = _disabled;
context._syncCaller = _syncCaller;
return context;
}
public override void OperationStarted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
_invalidOperationEncountered = true;
throw new InvalidOperationException(SR.GetString(SR.Async_operation_disabled));
}
Interlocked.Increment(ref _pendingCount);
#if DBG
Debug.Trace("Async", "OperationStarted(count=" + _pendingCount + ")");
Debug.Trace("AsyncStack", "OperationStarted(count=" + _pendingCount + ") from:\r\n" + System.Environment.StackTrace);
#endif
}
public override void OperationCompleted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
// throw from operation started could cause extra operation completed
return;
}
bool lastOperationCompleted = (Interlocked.Decrement(ref _pendingCount) == 0);
#if DBG
Debug.Trace("Async", "OperationCompleted(count=" + _pendingCount + ")");
Debug.Trace("AsyncStack", "OperationCompleted(count=" + _pendingCount + ") from:\r\n" + System.Environment.StackTrace);
#endif
if (lastOperationCompleted && _lastCompletionWorkItemCallback != null) {
// notify (once) about the last completion to resume the async work
WaitCallback cb = _lastCompletionWorkItemCallback;
_lastCompletionWorkItemCallback = null;
Debug.Trace("Async", "Queueing LastCompletionWorkItemCallback");
ThreadPool.QueueUserWorkItem(cb);
}
}
internal bool Enabled {
get { return !_disabled; }
}
internal void Enable() {
_disabled = false;
}
internal void Disable() {
_disabled = true;
}
internal void SetSyncCaller() {
_syncCaller = true;
}
internal void ResetSyncCaller() {
_syncCaller = false;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Threading;
using System.Web;
using System.Web.Util;
namespace System.Web {
internal class AspNetSynchronizationContext : SynchronizationContext {
private HttpApplication _application;
private bool _disabled;
private bool _syncCaller;
private bool _invalidOperationEncountered;
private int _pendingCount;
private Exception _error;
private WaitCallback _lastCompletionWorkItemCallback;
internal AspNetSynchronizationContext(HttpApplication app) {
_application = app;
}
private void CallCallback(SendOrPostCallback callback, Object state) {
// don't take app lock for [....] caller to avoid deadlocks in case they poll for result
if (_syncCaller) {
CallCallbackPossiblyUnderLock(callback, state);
}
else {
lock (_application) {
CallCallbackPossiblyUnderLock(callback, state);
}
}
}
private void CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) {
HttpApplication.ThreadContext threadContext = null;
try {
threadContext = _application.OnThreadEnter();
try {
callback(state);
}
catch (Exception e) {
_error = e;
}
}
finally {
if (threadContext != null) {
threadContext.Leave();
}
}
}
internal int PendingOperationsCount {
get { return _pendingCount; }
}
internal Exception Error {
get { return _error; }
}
internal void ClearError() {
_error = null;
}
internal void SetLastCompletionWorkItem(WaitCallback callback) {
Debug.Assert(_lastCompletionWorkItemCallback == null); // only one at a time
_lastCompletionWorkItemCallback = callback;
}
public override void Send(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Send");
Debug.Trace("AsyncStack", "Send from:\r\n" + System.Environment.StackTrace);
#endif
CallCallback(callback, state);
}
public override void Post(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Post");
Debug.Trace("AsyncStack", "Post from:\r\n" + System.Environment.StackTrace);
#endif
CallCallback(callback, state);
}
#if DBG
[EnvironmentPermission(SecurityAction.Assert, Unrestricted=true)]
private void CreateCopyDumpStack() {
Debug.Trace("Async", "CreateCopy");
Debug.Trace("AsyncStack", "CreateCopy from:\r\n" + System.Environment.StackTrace);
}
#endif
public override SynchronizationContext CreateCopy() {
#if DBG
CreateCopyDumpStack();
#endif
AspNetSynchronizationContext context = new AspNetSynchronizationContext(_application);
context._disabled = _disabled;
context._syncCaller = _syncCaller;
return context;
}
public override void OperationStarted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
_invalidOperationEncountered = true;
throw new InvalidOperationException(SR.GetString(SR.Async_operation_disabled));
}
Interlocked.Increment(ref _pendingCount);
#if DBG
Debug.Trace("Async", "OperationStarted(count=" + _pendingCount + ")");
Debug.Trace("AsyncStack", "OperationStarted(count=" + _pendingCount + ") from:\r\n" + System.Environment.StackTrace);
#endif
}
public override void OperationCompleted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
// throw from operation started could cause extra operation completed
return;
}
bool lastOperationCompleted = (Interlocked.Decrement(ref _pendingCount) == 0);
#if DBG
Debug.Trace("Async", "OperationCompleted(count=" + _pendingCount + ")");
Debug.Trace("AsyncStack", "OperationCompleted(count=" + _pendingCount + ") from:\r\n" + System.Environment.StackTrace);
#endif
if (lastOperationCompleted && _lastCompletionWorkItemCallback != null) {
// notify (once) about the last completion to resume the async work
WaitCallback cb = _lastCompletionWorkItemCallback;
_lastCompletionWorkItemCallback = null;
Debug.Trace("Async", "Queueing LastCompletionWorkItemCallback");
ThreadPool.QueueUserWorkItem(cb);
}
}
internal bool Enabled {
get { return !_disabled; }
}
internal void Enable() {
_disabled = false;
}
internal void Disable() {
_disabled = true;
}
internal void SetSyncCaller() {
_syncCaller = true;
}
internal void ResetSyncCaller() {
_syncCaller = false;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FieldToken.cs
- DesignerValidationSummaryAdapter.cs
- Registry.cs
- ModelItemCollectionImpl.cs
- AnimationClockResource.cs
- CqlParserHelpers.cs
- ContentElement.cs
- DescriptionAttribute.cs
- CommandEventArgs.cs
- FSWPathEditor.cs
- EventEntry.cs
- ComponentGuaranteesAttribute.cs
- ButtonField.cs
- ISAPIRuntime.cs
- SrgsNameValueTag.cs
- XmlSerializer.cs
- UseLicense.cs
- InvalidTimeZoneException.cs
- FixedPageStructure.cs
- DBSqlParserColumnCollection.cs
- OracleDataReader.cs
- ClientSideQueueItem.cs
- EntityCommandCompilationException.cs
- MappingModelBuildProvider.cs
- DataTransferEventArgs.cs
- hwndwrapper.cs
- LogStore.cs
- UnsafeNativeMethods.cs
- TextPenaltyModule.cs
- ClientScriptManager.cs
- DictionaryChange.cs
- RuleConditionDialog.cs
- DataContractFormatAttribute.cs
- GlyphsSerializer.cs
- SingleObjectCollection.cs
- EllipticalNodeOperations.cs
- XmlSignificantWhitespace.cs
- EntityClientCacheKey.cs
- PowerStatus.cs
- OleDbInfoMessageEvent.cs
- OLEDB_Enum.cs
- HelpPage.cs
- BatchParser.cs
- ZipIOLocalFileDataDescriptor.cs
- IndentedTextWriter.cs
- FunctionParameter.cs
- _FixedSizeReader.cs
- ReadOnlyCollection.cs
- TextOnlyOutput.cs
- ObjectItemAttributeAssemblyLoader.cs
- OleDbFactory.cs
- TextPointer.cs
- SortDescriptionCollection.cs
- DragDrop.cs
- PersistenceException.cs
- InheritanceContextChangedEventManager.cs
- WsrmFault.cs
- AppDomainFactory.cs
- FileInfo.cs
- Attributes.cs
- ResourceReferenceExpression.cs
- SourceFileBuildProvider.cs
- XomlCompilerError.cs
- EntityCommandCompilationException.cs
- MethodBuilderInstantiation.cs
- WebProxyScriptElement.cs
- RsaSecurityToken.cs
- OuterGlowBitmapEffect.cs
- EditorBrowsableAttribute.cs
- CompoundFileIOPermission.cs
- Int32RectConverter.cs
- HtmlInputPassword.cs
- Evaluator.cs
- DetailsViewDeletedEventArgs.cs
- ToolStripDropDown.cs
- ObjectViewQueryResultData.cs
- AutoResetEvent.cs
- WebResourceAttribute.cs
- DbConnectionPoolGroupProviderInfo.cs
- FormsAuthenticationUserCollection.cs
- AttributeAction.cs
- Switch.cs
- SplitterCancelEvent.cs
- InstalledFontCollection.cs
- BoundField.cs
- SmtpNegotiateAuthenticationModule.cs
- IpcPort.cs
- OletxVolatileEnlistment.cs
- XPathQilFactory.cs
- UnsafeNativeMethods.cs
- HtmlTableRowCollection.cs
- RuleSettings.cs
- ParameterEditorUserControl.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- _emptywebproxy.cs
- ProtectedConfiguration.cs
- BulletedListEventArgs.cs
- VirtualPathProvider.cs
- PropertyIDSet.cs
- ProvideValueServiceProvider.cs