新文章 网摘 文章 随笔 日记

C# 同步包含示例的上下文教程

C# 同步上下文 提供用于在各种同步模型中传播同步上下文的基本功能。

全名:

System.Threading.SynchronizationContext


下面的代码演示如何使用System.Threading 中的同步上下文。

例 1

namespace Microsoft.Web.Mvc {
    using System;
    using System.Threading;
    internal static class SynchronizationContextHelper {
        public static SynchronizationContext GetSynchronizationContext() {
            return SynchronizationContext.Current ?? new SynchronizationContext();
        }/* w   w w .    d e   mo    2 s   . c   om  */
        public static T Sync<T>(this SynchronizationContext syncContext, Func<T> func) {
            T theValue = default(T);
            Exception thrownException = null;
            syncContext.Send(o => {
                try {
                    theValue = func();
                }
                catch (Exception ex) {
                    thrownException = ex;
                }
            }, null);
            if (thrownException != null) {
                throw thrownException;
            }
            return theValue;
        }
        public static void Sync(this SynchronizationContext syncContext, Action action) {
            Sync<object>(syncContext, () => {
                action();
                return null;
            });
        }
    }
}

例 2

namespace System.Web.Mvc.Async {
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Threading;
    internal static class SynchronizationContextUtil {
        public static SynchronizationContext GetSynchronizationContext() {
            return SynchronizationContext.Current ?? new SynchronizationContext();
        }//   w w    w.   d e   m   o  2 s   . c  o m  
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is swallowed and immediately re-thrown")]
        public static T Sync<T>(this SynchronizationContext syncContext, Func<T> func) {
            T theValue = default(T);
            Exception thrownException = null;
            syncContext.Send(o =>
            {
                try {
                    theValue = func();
                }
                catch (Exception ex) {
                    thrownException = ex;
                }
            }, null);
            if (thrownException != null) {
                throw Error.SynchronizationContextUtil_ExceptionThrown(thrownException);
            }
            return theValue;
        }
        public static void Sync(this SynchronizationContext syncContext, Action action) {
            Sync<AsyncVoid>(syncContext, () =>
            {
                action();
                return default(AsyncVoid);
            });
        }
    }
}

例 3

using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace System.Web.Mvc.Async
{
    internal static class SynchronizationContextUtil
    {/* w w   w .    d e  m o  2    s.  c    o  m*/
        public static SynchronizationContext GetSynchronizationContext()
        {
            return SynchronizationContext.Current ?? new SynchronizationContext();
        }
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is swallowed and immediately re-thrown")]
        public static T Sync<T>(this SynchronizationContext syncContext, Func<T> func)
        {
            T theValue = default(T);
            Exception thrownException = null;
            syncContext.Send(o =>
            {
                try
                {
                    theValue = func();
                }
                catch (Exception ex)
                {
                    thrownException = ex;
                }
            }, null);
            if (thrownException != null)
            {
                throw Error.SynchronizationContextUtil_ExceptionThrown(thrownException);
            }
            return theValue;
        }
        public static void Sync(this SynchronizationContext syncContext, Action action)
        {
            Sync<AsyncVoid>(syncContext, () =>
            {
                action();
                return default(AsyncVoid);
            });
        }
    }
}
以前下一个
posted @ 2022-11-09 14:03  岭南春  阅读(83)  评论(0)    收藏  举报