本文整理汇总了C#中ManualResetEvent.Set方法的典型用法代码示例。如果您正苦于以下问题:C# ManualResetEvent.Set方法的具体用法?C# ManualResetEvent.Set怎么用?C# ManualResetEvent.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ManualResetEvent
的用法示例。
在下文中一共展示了ManualResetEvent.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestWaitAny
public void TestWaitAny()
{
int x;
e1 = new ManualResetEvent(false);
e2 = new ManualResetEvent(false);
x = WaitHandle.WaitAny(new WaitHandle[] {e1,e2}, 100,false);
AssertEquals("WaitAny(unset, unset)", x, WaitHandle.WaitTimeout);
e1.Set();
x = WaitHandle.WaitAny(new WaitHandle[] {e1,e2},100, false);
AssertEquals("WaitAny(set, unset)", x, 0);
e1.Reset();
e2.Set();
x = WaitHandle.WaitAny(new WaitHandle[] {e1,e2},100, false);
AssertEquals("WaitAny(set, unset)", x, 1);
e1.Set();
e2.Set();
x = WaitHandle.WaitAny(new WaitHandle[] {e1,e2},100, false);
AssertEquals("WaitAny(set, set)", x, 0);
}
示例2: Main
public static int Main ()
{
var mre_l = new ManualResetEvent (false);
var mre = new ManualResetEvent (false);
Func<string, Task<string>> f = async l =>
await Task.Factory.StartNew (() => {
if (!mre_l.WaitOne (3000))
throw new ApplicationException ("3");
return l;
});
var r = f ("a");
mre_l.Set ();
if (!r.Wait (3000))
return 1;
if (r.Result != "a")
return 11;
mre_l.Reset ();
Func<Task> ff = async () =>
await Task.Factory.StartNew (() => {
if (!mre_l.WaitOne (3000))
throw new ApplicationException ("3");
});
var rr = ff ();
mre_l.Set ();
if (!rr.Wait (3000))
return 2;
Func<short, Task<short>> f2 = async l => l;
var r2 = f2 (88);
if (r2.Result != 88)
return 3;
mre.Reset ();
mre_l.Reset ();
Action a = async () => await Task.Factory.StartNew (() => {
if (!mre_l.WaitOne (3000))
throw new ApplicationException ("4");
mre.Set ();
}, CancellationToken.None);
a ();
mre_l.Set ();
if (!mre.WaitOne (3000))
return 4;
Console.WriteLine ("ok");
return 0;
}
示例3: SetReset
public void SetReset()
{
using (ManualResetEvent mre = new ManualResetEvent(false))
{
Assert.False(mre.WaitOne(0));
mre.Set();
Assert.True(mre.WaitOne(0));
Assert.True(mre.WaitOne(0));
mre.Set();
Assert.True(mre.WaitOne(0));
mre.Reset();
Assert.False(mre.WaitOne(0));
}
}
示例4: Main
public static int Main ()
{
var mre = new ManualResetEvent (false);
await_mre = new ManualResetEvent (false);
var context = new MyContext (mre);
try {
SynchronizationContext.SetSynchronizationContext (context);
var t = Test ();
await_mre.Set ();
if (!t.Wait (3000))
return 3;
// Wait is needed because synchronization is executed as continuation (once task finished)
if (!mre.WaitOne (3000))
return 2;
} finally {
SynchronizationContext.SetSynchronizationContext (null);
}
if (context.Started != 0 || context.Completed != 0 || context.SendCounter != 0)
return 1;
Console.WriteLine ("ok");
return 0;
}
示例5: BlockOnSendBlockedIfReachedBoundedCapacity
public void BlockOnSendBlockedIfReachedBoundedCapacity()
{
var send1 = new ManualResetEvent(false);
var send2 = new ManualResetEvent(false);
var processor = new FakeMessageProcessor<Object>();
var bus = new OptimisticMessageSender<Object>(processor, 1);
Assert.Equal(1, bus.BoundedCapacity);
Task.Run(() =>
{
//NOTE: Once message is received for processing, the message is dequeued and thus we actually need
// one more message to confirm blocking behavior (i.e., one in queue and one being processed).
bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
send1.Set();
});
Task.Run(() =>
{
send1.WaitOne();
bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
send2.Set();
});
processor.WaitForMessage();
Assert.True(send1.WaitOne(TimeSpan.FromMilliseconds(100)));
Assert.False(send2.WaitOne(TimeSpan.FromMilliseconds(100)));
processor.ProcessNextMessage();
processor.WaitForMessage();
Assert.True(send2.WaitOne(TimeSpan.FromMilliseconds(100)));
}
示例6: PingPong
public void PingPong()
{
using (ManualResetEvent mre1 = new ManualResetEvent(true), mre2 = new ManualResetEvent(false))
{
const int Iters = 10;
Task.WaitAll(
Task.Factory.StartNew(() =>
{
for (int i = 0; i < Iters; i++)
{
Assert.True(mre1.WaitOne(FailedWaitTimeout));
mre1.Reset();
mre2.Set();
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default),
Task.Factory.StartNew(() =>
{
for (int i = 0; i < Iters; i++)
{
Assert.True(mre2.WaitOne(FailedWaitTimeout));
mre2.Reset();
mre1.Set();
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default));
}
}
示例7: Test2
static void Test2 (Process p)
{
StringBuilder sb = new StringBuilder ();
ManualResetEvent mre_output = new ManualResetEvent (false);
p.Start ();
p.OutputDataReceived += (s, a) => {
if (a.Data == null) {
mre_output.Set ();
return;
}
sb.Append (a.Data);
};
p.BeginOutputReadLine ();
if (!p.WaitForExit (1000))
Environment.Exit (4);
if (!mre_output.WaitOne (1000))
Environment.Exit (5);
if (sb.ToString () != "hello") {
Console.WriteLine ("process output = '{0}'", sb.ToString ());
Environment.Exit (6);
}
}
示例8: AbortSuspendTest
public static void AbortSuspendTest()
{
var e = new ManualResetEvent(false);
Action waitForThread;
var t = ThreadTestHelpers.CreateGuardedThread(out waitForThread, e.CheckedWait);
t.IsBackground = true;
Action verify = () =>
{
Assert.Throws<PlatformNotSupportedException>(() => t.Abort());
Assert.Throws<PlatformNotSupportedException>(() => t.Abort(t));
#pragma warning disable 618 // Obsolete members
Assert.Throws<PlatformNotSupportedException>(() => t.Suspend());
Assert.Throws<PlatformNotSupportedException>(() => t.Resume());
#pragma warning restore 618 // Obsolete members
};
verify();
t.Start();
verify();
e.Set();
waitForThread();
Assert.Throws<PlatformNotSupportedException>(() => Thread.ResetAbort());
}
示例9: AquireWillBlockIfAnotherLockAlreadyAquiredOnSameAggregate
public void AquireWillBlockIfAnotherLockAlreadyAquiredOnSameAggregate()
{
var correlationId = GuidStrategy.NewGuid();
var firstLockAquired = new ManualResetEvent(initialState: false);
var secondLockAquired = new ManualResetEvent(initialState: false);
var blockedTime = TimeSpan.Zero;
Task.Factory.StartNew(() =>
{
firstLockAquired.WaitOne();
using (var aggregateLock = new AggregateLock(typeof(Aggregate), correlationId))
{
var timer = Stopwatch.StartNew();
aggregateLock.Aquire();
timer.Stop();
blockedTime = timer.Elapsed;
secondLockAquired.Set();
}
});
using (var aggregateLock = new AggregateLock(typeof(Aggregate), correlationId))
{
aggregateLock.Aquire();
firstLockAquired.Set();
Thread.Sleep(100);
}
secondLockAquired.WaitOne();
Assert.InRange(blockedTime, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(150));
}
示例10: EvalPerf
private static void EvalPerf(Synch synch, int numThreads) {
ManualResetEvent startEvent = new ManualResetEvent(false);
int endTime = 0;
int totalOps = 0;
Thread[] threads = new Thread[numThreads];
for (int n = 0; n < numThreads; ++n) {
threads[n] = new Thread(() => {
int numOps = 0;
startEvent.WaitOne();
do {
for (int i = 0; i < LOCKS_PER_LOOP; ++i) {
synch.Synchronize(() => {
// Busy variant:
// Thread.Yield();
});
}
numOps += LOCKS_PER_LOOP;
} while (Environment.TickCount < endTime);
Interlocked.Add(ref totalOps, numOps);
});
threads[n].Start();
}
endTime = Environment.TickCount + MS_PER_TEST;
startEvent.Set();
foreach (Thread t in threads) {
t.Join();
}
Console.WriteLine("[{0}] {1}", numThreads, totalOps);
}
示例11: Test2
static void Test2 (Process p)
{
ManualResetEvent mre_output = new ManualResetEvent (false);
ManualResetEvent mre_error = new ManualResetEvent (false);
p.Start ();
p.OutputDataReceived += (s, a) => {
if (a.Data == null) {
mre_output.Set ();
return;
}
};
p.ErrorDataReceived += (s, a) => {
if (a.Data == null) {
mre_error.Set ();
return;
}
};
p.BeginOutputReadLine ();
p.BeginErrorReadLine ();
if (!p.WaitForExit (10000))
Environment.Exit (4);
if (!mre_output.WaitOne (1000))
Environment.Exit (5);
if (!mre_error.WaitOne (1000))
Environment.Exit (6);
}
示例12: Main
/* expected exit code: 255 */
static void Main (string[] args)
{
if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
ManualResetEvent mre = new ManualResetEvent (false);
var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
var ares = a.BeginInvoke (null, null);
if (!mre.WaitOne (5000))
Environment.Exit (2);
try {
a.EndInvoke (ares);
Environment.Exit (4);
} catch (CustomException) {
/* expected behaviour */
Environment.Exit (255);
} catch (Exception ex) {
Console.WriteLine (ex);
Environment.Exit (3);
}
Environment.Exit (5);
}
示例13: Main
/* expected exit code: 255 */
static void Main (string[] args)
{
if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
ManualResetEvent mre = new ManualResetEvent (false);
var t = Task.Factory.StartNew (new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
if (!mre.WaitOne (5000))
Environment.Exit (2);
try {
t.Wait ();
Environment.Exit (5);
} catch (AggregateException ae) {
Console.WriteLine (ae);
if (ae.InnerExceptions [0] is CustomException) {
/* expected behaviour */
Environment.Exit (255);
}
} catch (Exception ex) {
Console.WriteLine (ex);
Environment.Exit (3);
}
Environment.Exit (6);
}
示例14: Test1
static void Test1 (Process p)
{
ManualResetEvent mre_exit = new ManualResetEvent (false);
ManualResetEvent mre_output = new ManualResetEvent (false);
ManualResetEvent mre_error = new ManualResetEvent (false);
p.EnableRaisingEvents = true;
p.Exited += (s, a) => mre_exit.Set ();
p.Start ();
p.OutputDataReceived += (s, a) => {
if (a.Data == null) {
mre_output.Set ();
return;
}
};
p.ErrorDataReceived += (s, a) => {
if (a.Data == null) {
mre_error.Set ();
return;
}
};
p.BeginOutputReadLine ();
p.BeginErrorReadLine ();
if (!mre_exit.WaitOne (10000))
Environment.Exit (1);
if (!mre_output.WaitOne (1000))
Environment.Exit (2);
if (!mre_error.WaitOne (1000))
Environment.Exit (3);
}
示例15: BlockQueueUntilBelowBoundedCapacity
public void BlockQueueUntilBelowBoundedCapacity()
{
var monitor = new FakeMonitor();
var threadPool = new FakeThreadPool();
var blocked = new ManualResetEvent(false);
var released = new ManualResetEvent(false);
var taskScheduler = new BlockingThreadPoolTaskScheduler(1, threadPool, monitor);
monitor.BeforeWait = () => blocked.Set();
monitor.AfterPulse = () => released.Set();
Task.Factory.StartNew(() =>
{
// Schedule first task (non-blocking).
Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler);
// Schedule second task (blocking).
Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.AttachedToParent, taskScheduler);
});
// Wait for second task to be blocked.
Assert.True(blocked.WaitOne(TimeSpan.FromMilliseconds(100)));
Assert.Equal(1, threadPool.UserWorkItems.Count);
threadPool.RunNext();
// Wait for second task to be released.
Assert.True(released.WaitOne(TimeSpan.FromMilliseconds(100)));
threadPool.RunNext();
Assert.Equal(0, taskScheduler.ScheduledTasks.Count());
}