本文整理汇总了C#中System.Threading.CountdownEvent类的典型用法代码示例。如果您正苦于以下问题:C# CountdownEvent类的具体用法?C# CountdownEvent怎么用?C# CountdownEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CountdownEvent类属于System.Threading命名空间,在下文中一共展示了CountdownEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunDisruptorPass
protected override long RunDisruptorPass()
{
Setup();
var latch = new CountdownEvent(NUM_EVENT_PROCESSORS);
var listTh = new List<Thread>();
for (int i = 0; i < 3; i++)
{
handlers[i].Reset(latch, -1 + ITERATIONS);
}
disruptor.Start();
var start = System.Diagnostics.Stopwatch.StartNew();
for (long i = 0; i < ITERATIONS; i++)
{
long sequence = ringBuffer.Next();
ringBuffer[sequence].Value = i;
ringBuffer.Publish(sequence);
}
latch.Wait();
long opsPerSecond = (ITERATIONS * 1000L) / start.ElapsedMilliseconds;
for (int i = 0; i < NUM_EVENT_PROCESSORS; i++)
{
Assert.AreEqual(results[i], handlers[i].Value);
}
disruptor.Shutdown();
return opsPerSecond;
}
开发者ID:bingyang001,项目名称:disruptor-net-3.3.0-alpha,代码行数:28,代码来源:OnePublisherToThreeProcessorMultiCastThroughputTest.cs
示例2: ConcurrentRunner
public ConcurrentRunner(int maxThread, int loopEach)
{
this.maxThread = maxThread;
this.loopEach = loopEach;
this.semaphore = new SemaphoreSlim(0, maxThread);
this.countdown = new CountdownEvent(maxThread);
}
示例3: ExecuteReadWithParallel
protected PerformanceRecord ExecuteReadWithParallel(string operation, IEnumerable<uint> ids, int numberOfThreads, Func<long> readAction)
{
var countdownEvent = new CountdownEvent(numberOfThreads);
var sw = Stopwatch.StartNew();
var bytes = new long[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++)
{
var c = i;
ThreadPool.QueueUserWorkItem(
state =>
{
bytes[c] = readAction();
countdownEvent.Signal();
});
}
countdownEvent.Wait();
sw.Stop();
return new PerformanceRecord
{
Bytes = bytes.Sum(),
Operation = operation,
Time = DateTime.Now,
Duration = sw.ElapsedMilliseconds,
ProcessedItems = ids.Count() * numberOfThreads
};
}
示例4: MainWindow
public MainWindow()
{
InitializeComponent();
Thread.CurrentThread.Name = "Main Thread";
CountdownEvent folderEndByXML = new CountdownEvent(1);
CountdownEvent folderEndByTree = new CountdownEvent(1);
CountdownEvent fileEndByXML = new CountdownEvent(1);
CountdownEvent fileEndByTree = new CountdownEvent(1);
m_parser = new Parser(m_progressToken, folderEndByTree, folderEndByXML, fileEndByTree, fileEndByXML);
m_treeFiller = new TreeFiller(m_treeView, m_progressToken, folderEndByTree, fileEndByTree);
m_xmlFiller = new XMLFiller(m_progressToken, folderEndByXML, fileEndByXML);
m_folderName.DataContext = m_parser;
m_xmlFileName.DataContext = m_xmlFiller;
m_parser.ItemGrabbed += m_xmlFiller.ItemGrabbedHandler;
m_parser.ItemGrabbed += m_treeFiller.ItemGrabbedHandler;
m_parser.FolderStarted += m_xmlFiller.FolderStartedHandler;
m_parser.FolderStarted += m_treeFiller.FolderStartedHandler;
m_parser.FolderFinished += m_xmlFiller.FolderFinishedHandler;
m_parser.FolderFinished += m_treeFiller.FolderFinishedHandler;
m_parser.ParserFinishEvent += this.ParserFinishEventHandler;
m_xmlFiller.ExceptionOccuredEvent += this.ExceptionOccuredHandler;
m_treeFiller.ExceptionOccuredEvent += this.ExceptionOccuredHandler;
m_parser.ExceptionOccuredEvent += this.ExceptionOccuredHandler;
}
示例5: Start
public IDisposable Start()
{
_monitor.Start();
CountdownEvent pending = new CountdownEvent(_args.ConnectionLimit);
var interval = Observable.Interval(TimeSpan.FromSeconds(1))
.TakeWhile(_ => pending.CurrentCount > 0)
.Subscribe(async _ =>
{
var parallelCount = Math.Min(pending.CurrentCount, 10);
Task[] tasks = new Task[parallelCount];
for (int i = 0; i < parallelCount; i++)
{
tasks[i] = Task.Run(() => Connect(pending));
}
Task.WaitAll(tasks);
},
ex =>
{
Console.WriteLine(ex.Message);
Environment.Exit(1);
});
pending.Wait();
Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(this.SendMessage);
return null;
}
示例6: should_reconnect_within_5_seconds
public void should_reconnect_within_5_seconds()
{
const int total = 100;
var countdown = new CountdownEvent(total);
IBus producer = this.StartBus("producer", cfg => cfg.Route("boo"));
IBus consumer = this.StartBus(
"consumer",
cfg => cfg.On<BooMessage>("boo").
ReactWith(
(m, ctx) =>
{
Console.WriteLine("Received {0}.", m.Num);
countdown.Signal();
}));
int count = total;
while (count -- > 0)
{
producer.Emit("boo", new BooMessage(count));
Console.WriteLine("Sent {0}.", count);
Thread.Sleep(1.Seconds());
}
countdown.Wait();
}
示例7: Constructor_Zero
public void Constructor_Zero ()
{
var ce = new CountdownEvent (0);
Assert.IsTrue (ce.IsSet, "#1");
Assert.AreEqual (0, ce.InitialCount, "#2");
Assert.IsTrue (ce.Wait (0), "#3");
}
示例8: RunDisruptorPass
protected override long RunDisruptorPass()
{
CountdownEvent latch = new CountdownEvent(1);
long expectedCount = batchEventProcessor.Sequence.Value + ITERATIONS;
handler.Reset(latch, ITERATIONS);
Task.Factory.StartNew(() => batchEventProcessor.Run());
Stopwatch start = Stopwatch.StartNew();
RingBuffer<long[]> rb = ringBuffer;
for (long i = 0; i < ITERATIONS; i++)
{
long next = rb.Next();
long[] @event = rb.Get(next);
for (int j = 0; j < @event.Length; j++)
{
@event[j] = i;
}
rb.Publish(next);
}
latch.Wait();
long opsPerSecond = (ITERATIONS * ARRAY_SIZE * 1000L) / (start.ElapsedMilliseconds);
waitForEventProcessorSequence(expectedCount);
batchEventProcessor.Halt();
PerfTestUtil.failIf(0, handler.Value);
return opsPerSecond;
}
开发者ID:bingyang001,项目名称:disruptor-net-3.3.0-alpha,代码行数:30,代码来源:OneToOneSequencedLongArrayThroughputTest.cs
示例9: Should_be_able_to_subscribe_as_exlusive
public void Should_be_able_to_subscribe_as_exlusive()
{
var countdownEvent = new CountdownEvent(10);
var firstCount = 0;
var secondCount = 0;
bus.Subscribe<MyMessage>("test", message =>
{
countdownEvent.Signal();
Interlocked.Increment(ref firstCount);
Console.WriteLine("[1] " + message.Text);
}, x => x.AsExclusive());
bus.Subscribe<MyMessage>("test", message =>
{
countdownEvent.Signal();
Interlocked.Increment(ref secondCount);
Console.WriteLine("[2] " + message.Text);
}, x => x.AsExclusive());
for (var i = 0; i < 10; ++i)
bus.Publish(new MyMessage
{
Text = "Exclusive " + i
});
countdownEvent.Wait(10 * 1000);
Assert.IsTrue(firstCount == 10 && secondCount == 0 || firstCount == 0 && secondCount == 10);
Console.WriteLine("Stopped consuming");
}
示例10: TestTimerStartAutoReset
public void TestTimerStartAutoReset()
{
CountdownEvent cde = new CountdownEvent(1);
int result = 0;
_timer = new TestTimer(1);
// Test defaults.
Assert.Equal(1, _timer.Interval);
Assert.True(_timer.AutoReset);
_timer.AutoReset = false;
_timer.Elapsed += (sender, e) => { result = ++result; cde.Signal(); };
_timer.Start();
Assert.True(_timer.Enabled);
cde.Wait();
// Only elapsed once.
Assert.Equal(1, result);
cde = new CountdownEvent(10);
_timer.AutoReset = true;
cde.Wait();
cde.Dispose();
_timer.Stop();
// Atleast elapsed 10 times.
Assert.True(result >= 10);
}
示例11: Run
private static void Run(string[] args)
{
Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");
var config = new ClientConfig();
config.GetNetworkConfig().AddAddress("127.0.0.1");
var client = HazelcastClient.NewHazelcastClient(config);
var list = client.GetList<string>("collection-listener-example");
var cdown = new CountdownEvent(3);
list.AddItemListener(new ItemListener<string>
{
OnItemAdded = e =>
{
Console.WriteLine("Item added: " + e.GetItem());
cdown.Signal();
},
OnItemRemoved = e =>
{
Console.WriteLine("Item removed: " + e.GetItem());
cdown.Signal();
}
}, true);
list.Add("item1");
list.Add("item2");
list.Remove("item1");
cdown.Wait();
list.Destroy();
client.Shutdown();
}
示例12: M1
static void M1()
{
var sameLocalVariable = 123;
var cdevent = new CountdownEvent(2);
if (Fork.CloneThread())
{
lock (_sync)
{
Console.ReadKey();
Console.WriteLine("in forked thread: {0}, tid: {1} ", sameLocalVariable, Thread.CurrentThread.ManagedThreadId);
cdevent.Signal();
}
}
else
{
lock (_sync)
{
Console.ReadKey();
Console.WriteLine("in parent thread: {0}, tid: {1} ", sameLocalVariable, Thread.CurrentThread.ManagedThreadId);
cdevent.Signal();
}
}
cdevent.Wait();
}
示例13: be_able_to_subscribe_to_non_existing_stream_and_then_catch_event
public void be_able_to_subscribe_to_non_existing_stream_and_then_catch_event()
{
const string stream = "be_able_to_subscribe_to_non_existing_stream_and_then_catch_event";
using (var store = BuildConnection(_node))
{
store.ConnectAsync().Wait();
var appeared = new CountdownEvent(1);
var dropped = new CountdownEvent(1);
var subscription = store.SubscribeToStreamFrom(stream,
null,
CatchUpSubscriptionSettings.Default,
(_, x) => appeared.Signal(),
_ => Log.Info("Live processing started."),
(_, __, ___) => dropped.Signal());
store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Wait();
if (!appeared.Wait(Timeout))
{
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
Assert.Fail("Appeared countdown event timed out.");
}
Assert.IsFalse(dropped.Wait(0));
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
}
}
示例14: be_able_to_subscribe_to_non_existing_stream
public void be_able_to_subscribe_to_non_existing_stream()
{
const string stream = "be_able_to_subscribe_to_non_existing_stream";
using (var store = BuildConnection(_node))
{
store.ConnectAsync().Wait();
var appeared = new ManualResetEventSlim(false);
var dropped = new CountdownEvent(1);
var subscription = store.SubscribeToStreamFrom(stream,
null,
CatchUpSubscriptionSettings.Default,
(_, x) => appeared.Set(),
_ => Log.Info("Live processing started."),
(_, __, ___) => dropped.Signal());
Thread.Sleep(100); // give time for first pull phase
store.SubscribeToStreamAsync(stream, false, (s, x) => { }, (s, r, e) => { }).Wait();
Thread.Sleep(100);
Assert.IsFalse(appeared.Wait(0), "Some event appeared.");
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
}
}
示例15: HydratingFromMultipleThreads_IsSafe
public void HydratingFromMultipleThreads_IsSafe()
{
var numberOfHydrations = 0;
var cache = new CachedValue<int>(() => ++numberOfHydrations);
Assert.That(cache.Value, Is.EqualTo(1));
using (var countdownEvent = new CountdownEvent(2))
{
Action threadAction = () =>
{
cache.Invalidate();
countdownEvent.Signal();
countdownEvent.Wait();
Assert.That(cache.Value, Is.EqualTo(2));
};
var t1 = threadAction.BeginInvoke(threadAction.EndInvoke, null);
var t2 = threadAction.BeginInvoke(threadAction.EndInvoke, null);
WaitHandle.WaitAll(new[]{t1.AsyncWaitHandle, t2.AsyncWaitHandle});
}
Assert.That(numberOfHydrations, Is.EqualTo(2));
}