本文整理汇总了C#中ManualResetEvent.CheckedWait方法的典型用法代码示例。如果您正苦于以下问题:C# ManualResetEvent.CheckedWait方法的具体用法?C# ManualResetEvent.CheckedWait怎么用?C# ManualResetEvent.CheckedWait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ManualResetEvent
的用法示例。
在下文中一共展示了ManualResetEvent.CheckedWait方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerifyExecutionContextFlow
private static void VerifyExecutionContextFlow(AsyncLocal<int> asyncLocal, int expectedValue)
{
Assert.Equal(expectedValue == 0, ExecutionContext.IsFlowSuppressed());
if (ExecutionContext.IsFlowSuppressed())
{
Assert.Null(ExecutionContext.Capture());
}
VerifyExecutionContext(ExecutionContext.Capture(), asyncLocal, expectedValue);
int asyncLocalValue = -1;
var done = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
state =>
{
asyncLocalValue = asyncLocal.Value;
done.Set();
});
done.CheckedWait();
Assert.Equal(expectedValue, asyncLocalValue);
}
示例2: WaitNotificationTest
public static void WaitNotificationTest()
{
ThreadTestHelpers.RunTestInBackgroundThread(() =>
{
var tsc = new TestSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(tsc);
Assert.Same(tsc, SynchronizationContext.Current);
var e = new ManualResetEvent(false);
tsc.WaitAction = () => e.Set();
Assert.False(tsc.IsWaitNotificationRequired());
Assert.False(e.WaitOne(0));
tsc.SetWaitNotificationRequired();
Assert.True(tsc.IsWaitNotificationRequired());
Assert.True(e.WaitOne(0));
var mres = new ManualResetEventSlim();
tsc.WaitAction = () => mres.Set();
mres.Reset();
mres.CheckedWait();
e.Reset();
tsc.WaitAction = () => e.Set();
SynchronizationContext.SetSynchronizationContext(new TestSynchronizationContext());
Assert.False(e.WaitOne(0));
SynchronizationContext.SetSynchronizationContext(tsc);
Assert.True(e.WaitOne(0));
e.Reset();
e.CheckedWait();
e.Reset();
var lockObj = new object();
var lockAcquiredFromBackground = new AutoResetEvent(false);
Action waitForThread;
Thread t =
ThreadTestHelpers.CreateGuardedThread(out waitForThread, () =>
{
lock (lockObj)
{
lockAcquiredFromBackground.Set();
e.CheckedWait();
}
});
t.IsBackground = true;
t.Start();
lockAcquiredFromBackground.CheckedWait();
Assert.True(Monitor.TryEnter(lockObj, ThreadTestHelpers.UnexpectedTimeoutMilliseconds));
Monitor.Exit(lockObj);
waitForThread();
e.Reset();
var m = new Mutex();
t = ThreadTestHelpers.CreateGuardedThread(out waitForThread, () =>
{
m.CheckedWait();
try
{
lockAcquiredFromBackground.Set();
e.CheckedWait();
}
finally
{
m.ReleaseMutex();
}
});
t.IsBackground = true;
t.Start();
lockAcquiredFromBackground.CheckedWait();
m.CheckedWait();
m.ReleaseMutex();
waitForThread();
});
}
示例3: WaitingUpgradersTest
public static void WaitingUpgradersTest()
{
var trwl = new TestReaderWriterLock();
trwl.AcquireReaderLock();
var waitingUpgrader1AcquiredReadLock = new ManualResetEvent(false);
Action waitForWaitingUpgrader1, waitForWaitingUpgrader2, waitForWaitingUpgrader3;
Thread waitingUpgrader1 =
ThreadTestHelpers.CreateGuardedThread(out waitForWaitingUpgrader1, () =>
{
trwl.AcquireReaderLock();
waitingUpgrader1AcquiredReadLock.Set();
TestLockCookie tlc = trwl.UpgradeToWriterLock();
trwl.DowngradeFromWriterLock(tlc);
trwl.ReleaseReaderLock();
});
Action upgradeDowngradeLock =
() =>
{
TestLockCookie tlc = trwl.UpgradeToWriterLock();
trwl.DowngradeFromWriterLock(tlc);
};
Thread waitingUpgrader2 = ThreadTestHelpers.CreateGuardedThread(out waitForWaitingUpgrader2, upgradeDowngradeLock);
Thread waitingUpgrader3 = ThreadTestHelpers.CreateGuardedThread(out waitForWaitingUpgrader3, upgradeDowngradeLock);
waitingUpgrader1.IsBackground = true;
waitingUpgrader2.IsBackground = true;
waitingUpgrader1.Start();
waitingUpgrader1AcquiredReadLock.CheckedWait();
waitingUpgrader2.Start();
waitingUpgrader3.Start();
ThreadTestHelpers.WaitForCondition(() => (waitingUpgrader1.ThreadState & ThreadState.WaitSleepJoin) != 0);
ThreadTestHelpers.WaitForCondition(() => (waitingUpgrader2.ThreadState & ThreadState.WaitSleepJoin) != 0);
ThreadTestHelpers.WaitForCondition(() => (waitingUpgrader3.ThreadState & ThreadState.WaitSleepJoin) != 0);
// Releasing the read lock releases a waiting upgrader, that writer downgrades its write lock, in turn releasing the
// other upgrader, and so on
trwl.ReleaseReaderLock();
waitForWaitingUpgrader1();
waitForWaitingUpgrader2();
waitForWaitingUpgrader3();
trwl.Dispose();
}
示例4: ThreadStateTest
public static void ThreadStateTest()
{
var e0 = new ManualResetEvent(false);
var e1 = new AutoResetEvent(false);
Action waitForThread;
var t =
ThreadTestHelpers.CreateGuardedThread(out waitForThread, () =>
{
e0.CheckedWait();
ThreadTestHelpers.WaitForConditionWithoutBlocking(() => e1.WaitOne(0));
});
Assert.Equal(ThreadState.Unstarted, t.ThreadState);
t.IsBackground = true;
Assert.Equal(ThreadState.Unstarted | ThreadState.Background, t.ThreadState);
t.Start();
ThreadTestHelpers.WaitForCondition(() => t.ThreadState == (ThreadState.WaitSleepJoin | ThreadState.Background));
e0.Set();
ThreadTestHelpers.WaitForCondition(() => t.ThreadState == (ThreadState.Running | ThreadState.Background));
e1.Set();
waitForThread();
Assert.Equal(ThreadState.Stopped, t.ThreadState);
t = ThreadTestHelpers.CreateGuardedThread(
out waitForThread,
() => ThreadTestHelpers.WaitForConditionWithoutBlocking(() => e1.WaitOne(0)));
t.Start();
ThreadTestHelpers.WaitForCondition(() => t.ThreadState == ThreadState.Running);
e1.Set();
waitForThread();
Assert.Equal(ThreadState.Stopped, t.ThreadState);
}
示例5: JoinTest
public static void JoinTest()
{
var threadReady = new ManualResetEvent(false);
var continueThread = new ManualResetEvent(false);
Action waitForThread;
var t =
ThreadTestHelpers.CreateGuardedThread(out waitForThread, () =>
{
threadReady.Set();
continueThread.CheckedWait();
Thread.Sleep(ExpectedTimeoutMilliseconds);
});
t.IsBackground = true;
Assert.Throws<ArgumentOutOfRangeException>(() => t.Join(-2));
Assert.Throws<ArgumentOutOfRangeException>(() => t.Join(TimeSpan.FromMilliseconds(-2)));
Assert.Throws<ArgumentOutOfRangeException>(() => t.Join(TimeSpan.FromMilliseconds((double)int.MaxValue + 1)));
Assert.Throws<ThreadStateException>(() => t.Join());
Assert.Throws<ThreadStateException>(() => t.Join(UnexpectedTimeoutMilliseconds));
Assert.Throws<ThreadStateException>(() => t.Join(TimeSpan.FromMilliseconds(UnexpectedTimeoutMilliseconds)));
t.Start();
threadReady.CheckedWait();
Assert.False(t.Join(ExpectedTimeoutMilliseconds));
Assert.False(t.Join(TimeSpan.FromMilliseconds(ExpectedTimeoutMilliseconds)));
continueThread.Set();
waitForThread();
Assert.True(t.Join(0));
Assert.True(t.Join(TimeSpan.Zero));
}
示例6: IsThreadPoolThreadTest
public static void IsThreadPoolThreadTest()
{
var isThreadPoolThread = false;
Thread t = null;
t = new Thread(() => { isThreadPoolThread = t.IsThreadPoolThread; });
t.IsBackground = true;
Assert.False(t.IsThreadPoolThread);
t.Start();
t.Join(UnexpectedTimeoutMilliseconds);
Assert.False(isThreadPoolThread);
var e = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
state =>
{
isThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread;
e.Set();
});
e.CheckedWait();
Assert.True(isThreadPoolThread);
}