linux-packaging-mono/mcs/class/System.Web.Mvc3/Mvc/Async/SynchronizationContextUtil.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

48 lines
1.8 KiB
C#

namespace System.Web.Mvc.Async {
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
internal static class SynchronizationContextUtil {
public static SynchronizationContext GetSynchronizationContext() {
// In a runtime environment, SynchronizationContext.Current will be set to an instance
// of AspNetSynchronizationContext. In a unit test environment, the Current property
// won't be set and we have to create one on the fly.
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) {
// by default, the AspNetSynchronizationContext type will swallow thrown exceptions,
// so we need to save and propagate them
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);
});
}
}
}