private void Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture() { bool result = false; Assert.IsFalse(success, "pre-check"); ExecutionContext ec = GetExecutionContextUnmanaged(); AsyncFlowControl afc = ExecutionContext.SuppressFlow(); try { ExecutionContext.Run(ec, new ContextCallback(Callback), true); } catch (SecurityException) { result = true; } finally { Assert.IsFalse(success, "post-check"); afc.Undo(); Assert.IsTrue(result, "Result"); } }
private void Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture() { bool result = false; Assert.IsFalse(success, "pre-check"); AsyncFlowControl afc = SecurityContext.SuppressFlow(); try { SecurityContext sc = GetSecurityContextUnmanaged(); SecurityContext.Run(sc, new ContextCallback(Callback), true); } catch (InvalidOperationException) { result = true; } finally { Assert.IsFalse(success, "post-check"); afc.Undo(); Assert.IsTrue(result, "Result"); } }
private static T EscapeExecutionContext <T>(Func <T> callback) { AsyncFlowControl suppressExecutionContextFlow = ExecutionContext.SuppressFlow(); T retval2; try { T retval = default(T); Exception threadException = null; Thread thread = new Thread(new ThreadStart(delegate { try { try { SCryptAlgorithm.SafeSetPrincipal(null); retval = callback(); } catch (Exception e) { threadException = e; } } catch { } })); thread.Start(); thread.Join(); if (threadException != null) { throw new TargetInvocationException(threadException); } retval2 = retval; } finally { suppressExecutionContextFlow.Undo(); } return(retval2); }
internal static SecurityContext CaptureSecurityContextNoIdentityFlow() { SecurityContext securityContext; if (!SecurityContext.IsWindowsIdentityFlowSuppressed()) { AsyncFlowControl asyncFlowControl = SecurityContext.SuppressFlowWindowsIdentity(); try { securityContext = SecurityContext.Capture(); } finally { asyncFlowControl.Dispose(); } return(securityContext); } else { return(SecurityContext.Capture()); } }
public void SuppressFlow_Both_Undo() { Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-1"); Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1"); AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity(); Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2"); Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2"); AsyncFlowControl afc2 = SecurityContext.SuppressFlow(); Assert.IsTrue(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2"); Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2"); afc2.Undo(); // note: afc2 Undo return to the original (not the previous) state Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2"); Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2"); // we can't use the first AsyncFlowControl afc.Undo(); }
static void Main(string[] args) { new Thread(() => { Console.WriteLine("i-0:" + _localThread.Value); }).Start(); new Thread(() => { Console.WriteLine("i-1:" + _localThread.Value); }).Start(); AsyncFlowControl aFC = ExecutionContext.SuppressFlow(); Console.WriteLine("Is the flow suppressed? " + ExecutionContext.IsFlowSuppressed()); Console.WriteLine("Restore the flow."); aFC.Undo(); Console.WriteLine("Is the flow suppressed? " + ExecutionContext.IsFlowSuppressed()); Console.ReadKey(); }
public static void AsyncFlowControlTest() { ThreadTestHelpers.RunTestInBackgroundThread(() => { Action<AsyncFlowControl, AsyncFlowControl, bool> verifyEquality = (afc0, afc1, areExpectedToBeEqual) => { Assert.Equal(areExpectedToBeEqual, afc0.Equals(afc1)); Assert.Equal(areExpectedToBeEqual, afc0.Equals((object)afc1)); Assert.Equal(areExpectedToBeEqual, afc0 == afc1); Assert.NotEqual(areExpectedToBeEqual, afc0 != afc1); }; AsyncFlowControl asyncFlowControl0 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); AsyncFlowControl asyncFlowControl1 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); verifyEquality(asyncFlowControl0, asyncFlowControl1, true); verifyEquality(asyncFlowControl1, asyncFlowControl0, true); var asyncLocal = new AsyncLocal<int>(); asyncLocal.Value = 1; asyncFlowControl1 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); verifyEquality(asyncFlowControl0, asyncFlowControl1, true); verifyEquality(asyncFlowControl1, asyncFlowControl0, true); asyncFlowControl1 = new AsyncFlowControl(); verifyEquality(asyncFlowControl0, asyncFlowControl1, false); verifyEquality(asyncFlowControl1, asyncFlowControl0, false); ThreadTestHelpers.RunTestInBackgroundThread(() => asyncFlowControl1 = ExecutionContext.SuppressFlow()); verifyEquality(asyncFlowControl0, asyncFlowControl1, false); verifyEquality(asyncFlowControl1, asyncFlowControl0, false); }); }
public static void AsyncFlowControlTest() { ThreadTestHelpers.RunTestInBackgroundThread(() => { Action <AsyncFlowControl, AsyncFlowControl, bool> verifyEquality = (afc0, afc1, areExpectedToBeEqual) => { Assert.Equal(areExpectedToBeEqual, afc0.Equals(afc1)); Assert.Equal(areExpectedToBeEqual, afc0.Equals((object)afc1)); Assert.Equal(areExpectedToBeEqual, afc0 == afc1); Assert.NotEqual(areExpectedToBeEqual, afc0 != afc1); }; AsyncFlowControl asyncFlowControl0 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); AsyncFlowControl asyncFlowControl1 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); verifyEquality(asyncFlowControl0, asyncFlowControl1, true); verifyEquality(asyncFlowControl1, asyncFlowControl0, true); var asyncLocal = new AsyncLocal <int>(); asyncLocal.Value = 1; asyncFlowControl1 = ExecutionContext.SuppressFlow(); ExecutionContext.RestoreFlow(); verifyEquality(asyncFlowControl0, asyncFlowControl1, true); verifyEquality(asyncFlowControl1, asyncFlowControl0, true); asyncFlowControl1 = new AsyncFlowControl(); verifyEquality(asyncFlowControl0, asyncFlowControl1, false); verifyEquality(asyncFlowControl1, asyncFlowControl0, false); ThreadTestHelpers.RunTestInBackgroundThread(() => asyncFlowControl1 = ExecutionContext.SuppressFlow()); verifyEquality(asyncFlowControl0, asyncFlowControl1, false); verifyEquality(asyncFlowControl1, asyncFlowControl0, false); }); }
void UsingDisposeableStruct() { using (AsyncFlowControl flow = new AsyncFlowControl()) { } }
public static bool op_Inequality(AsyncFlowControl a, AsyncFlowControl b) { }
public bool Equals(AsyncFlowControl obj) { }
public SuppressFlow() { _undoer = ExecutionContext.SuppressFlow(); }
public AsyncFlowControlHelper(AsyncFlowControl asyncFlowControl) { _asyncFlowControl = asyncFlowControl; }
public static bool op_Inequality(AsyncFlowControl a, AsyncFlowControl b) {}
public bool Equals(AsyncFlowControl obj) {}