本文整理汇总了C#中System.Threading.CountdownEvent.Signal方法的典型用法代码示例。如果您正苦于以下问题:C# CountdownEvent.Signal方法的具体用法?C# CountdownEvent.Signal怎么用?C# CountdownEvent.Signal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.CountdownEvent
的用法示例。
在下文中一共展示了CountdownEvent.Signal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var customers = Enumerable.Range(1, 20);
using (var countdown = new CountdownEvent(1))
{
foreach (var customer in customers)
{
int currentCustomer = customer;
countdown.AddCount();
ThreadPool.QueueUserWorkItem(delegate
{
BuySomeStuff(currentCustomer);
countdown.Signal();
});
}
countdown.Signal();
countdown.Wait();
}
Console.WriteLine("All Customers finished shopping...");
Console.ReadKey();
}
示例2: BasicUsageTest
public void BasicUsageTest ()
{
bool act1 = false, act2 = false;
var evt = new CountdownEvent (2);
var broadcast = new BroadcastBlock<int> (null);
var action1 = new ActionBlock<int> (i =>
{
act1 = i == 42;
evt.Signal ();
});
var action2 = new ActionBlock<int> (i =>
{
act2 = i == 42;
evt.Signal ();
});
broadcast.LinkTo (action1);
broadcast.LinkTo (action2);
Assert.IsTrue (broadcast.Post (42));
Assert.IsTrue (evt.Wait (100));
Assert.IsTrue (act1);
Assert.IsTrue (act2);
}
示例3: TestGetPointsByMetadata
public void TestGetPointsByMetadata()
{
RunAndAwait( () =>
{
double startingLat = 20;
double startingLong = 10;
int maxPoints = 10;
SetDefinedCategory( GetRandomCategory() );
Dictionary<String, String> meta = new Dictionary<string, string>();
meta.Add( "object_type_sync", "office" );
CountdownEvent latch = new CountdownEvent( maxPoints );
for( int i = 0; i < maxPoints; i++ )
{
Backendless.Geo.SavePoint( startingLat, startingLong + i, GetDefinedCategories(), meta,
new AsyncCallback<GeoPoint>( response => latch.Signal(), fault =>
{
for( int j = 0; j < latch.CurrentCount; j++ )
latch.Signal();
} ) );
}
latch.Wait();
GetCollectionAndCheck( startingLat, startingLong, maxPoints, maxPoints, meta, new BackendlessGeoQuery(meta) );
} );
}
示例4: Run
public 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 map = client.GetMap<string, string>("listener-example");
var cdown = new CountdownEvent(2);
map.AddEntryListener(new EntryAdapter<string, string>
{
Added = e =>
{
Console.WriteLine("Key '{0}' with value ' {1}' was added.", e.GetKey(), e.GetValue());
cdown.Signal();
},
Removed = e =>
{
Console.WriteLine("Key '{0}' with value ' {1}' was removed.", e.GetKey(), e.GetOldValue());
cdown.Signal();
}
}, true);
map.Put("key", "value");
map.Remove("key");
cdown.Wait();
map.Destroy();
}
示例5: TestGetPointsForRectangle
public void TestGetPointsForRectangle()
{
RunAndAwait( () =>
{
double startingLat = 10;
double startingLong = 10;
int maxPoints = 10;
SetDefinedCategory( GetRandomCategory() );
Dictionary<String, String> meta = GetRandomSimpleMetadata();
CountdownEvent latch = new CountdownEvent( maxPoints );
for( int i = 0; i < maxPoints; i++ )
{
Backendless.Geo.SavePoint( startingLat, startingLong + i, GetDefinedCategories(), meta,
new AsyncCallback<GeoPoint>( response => latch.Signal(), fault =>
{
for( int j = 0; j < latch.CurrentCount; j++ )
latch.Signal();
} ) );
}
latch.Wait();
var geoQuery = new BackendlessGeoQuery( startingLat + 1, startingLong - 1, startingLat - 1,
startingLong + maxPoints + 1 );
GetCollectionAndCheck( startingLat, startingLong, maxPoints, maxPoints, meta, geoQuery );
} );
}
示例6: 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();
}
示例7: 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();
}
示例8: CurrentThreadSchedulerRecursiveSchedulingExample
private static void CurrentThreadSchedulerRecursiveSchedulingExample()
{
Demo.DisplayHeader("CurrentThreadScheduler - Recursive scheduling will queue the action on caller thread");
var currentThreadScheduler = CurrentThreadScheduler.Instance;
var countdownEvent = new CountdownEvent(2);
currentThreadScheduler.Schedule(Unit.Default,
(s, _) =>
{
Console.WriteLine("Outer Action - Thread:{0}", Thread.CurrentThread.ManagedThreadId);
s.Schedule(Unit.Default,
(s2, __) =>
{
Console.WriteLine("Inner Action - Thread:{0}", Thread.CurrentThread.ManagedThreadId);
countdownEvent.Signal();
Console.WriteLine("Inner Action - Done:{0}", Thread.CurrentThread.ManagedThreadId);
return Disposable.Empty;
});
countdownEvent.Signal();
Console.WriteLine("Outer Action - Done");
return Disposable.Empty;
});
countdownEvent.Wait();
}
示例9: ExecuteLevel
protected override void ExecuteLevel(IList<Computation> computationsOfLevel)
{
using (var countEvent = new CountdownEvent(1))
{
foreach (var item in computationsOfLevel)
{
var cc = item.Context as ParallelComputationContext;
if (cc != null)
{
countEvent.AddCount();
cc.RunTransform(() =>
{
item.Transform();
countEvent.Signal();
});
}
else
{
countEvent.Signal();
countEvent.Wait();
item.Transform();
countEvent.Reset();
}
OnComputationCompleted(new ComputationEventArgs(item));
}
countEvent.Signal();
countEvent.Wait();
}
}
示例10: Remove_WhenConcurrentDeletesUsingDtc_OnlyOneOperationShouldSucceed
public async Task Remove_WhenConcurrentDeletesUsingDtc_OnlyOneOperationShouldSucceed()
{
var persister = new TimeoutPersister(store);
var timeoutData = new TimeoutData();
await persister.Add(timeoutData, new ContextBag());
var documentRemoved = new CountdownEvent(2);
var t1 = Task.Run(async () =>
{
using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
{
var result = await persister.TryRemove(timeoutData.Id, new ContextBag());
documentRemoved.Signal(1);
documentRemoved.Wait();
tx.Complete();
return result;
}
});
var t2 = Task.Run(async () =>
{
using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
{
var result = await persister.TryRemove(timeoutData.Id, new ContextBag());
documentRemoved.Signal(1);
documentRemoved.Wait();
tx.Complete();
return result;
}
});
Assert.IsTrue(await t1 | await t2, "the document should be deleted");
Assert.IsFalse(t1.Result && t2.Result, "only one operation should complete successfully");
}
示例11: TestCreate_Running_And_Stop
public void TestCreate_Running_And_Stop()
{
using (var target = new ConsumerWorker<int, int>())
using (var counter = new CountdownEvent(3))
using (var completed = new CountdownEvent(1))
{
target.Init((k, i) => { counter.Signal(); }, Assert.IsNull);
var q = new MockQueue {1, 2};
target.Run(q, w =>
{
Assert.AreSame(w, target);
completed.Signal();
});
Assert.IsTrue(target.IsRunning);
Assert.IsTrue(completed.Wait(1000)); //stop is not safe to call before completed (by design)
target.Stop(w =>
{
Assert.AreSame(w, target);
counter.Signal();
});
Assert.IsTrue(counter.Wait(1000));
Assert.IsFalse(target.IsRunning);
}
}
示例12: Run
private static void Run()
{
using (var countdown = new CountdownEvent(2))
{
var subscriber = new Subscriber(MqttBrokerAddress, MqttBrokerPort);
subscriber.Subscribe(Topic.Hello, Topic.Goodbye);
subscriber.OnMessage += (topic, payload) =>
{
if (topic == Topic.Hello)
{
var msg = JsonConvert.DeserializeObject<HelloMessage>(payload);
_log.Info("Topic: " + topic);
_log.Info("Message: " + msg);
countdown.Signal();
}
else if (topic == Topic.Goodbye)
{
var msg = JsonConvert.DeserializeObject<GoodbyeMessage>(payload);
_log.Info("Topic: " + topic);
_log.Info("Message: " + msg);
countdown.Signal();
}
};
var publisher = new Publisher(MqttBrokerAddress, MqttBrokerPort);
publisher.Publish(Topic.Hello, new HelloMessage() { Name = "John Smith", Date = DateTime.Now });
publisher.Publish(Topic.Goodbye, new GoodbyeMessage() { Name = "Jane Smith", Date = DateTime.Now });
countdown.Wait();
}
}
示例13: TestFindAllEntities
public void TestFindAllEntities()
{
RunAndAwait( () =>
{
var entities = new List<FindAllEntityAsync>();
var latch = new CountdownEvent( 10 );
for( int i = 0; i < 10; i++ )
{
var findAllEntity = new FindAllEntityAsync {Name = "bot_#" + i, Age = 20 + i};
Backendless.Persistence.Save( findAllEntity, new AsyncCallback<FindAllEntityAsync>( response =>
{
entities.Add( findAllEntity );
latch.Signal();
}, fault =>
{
for( int j = 0; j < latch.CurrentCount; j++ )
latch.Signal();
FailCountDownWith( fault );
} ) );
}
latch.Wait();
Backendless.Persistence.Of<FindAllEntityAsync>()
.Find( new ResponseCallback<BackendlessCollection<FindAllEntityAsync>>( this )
{
ResponseHandler =
backendlessCollection => AssertArgumentAndResultCollections( entities, backendlessCollection )
} );
} );
}
示例14: Should_cosume_multiple_message_types
public void Should_cosume_multiple_message_types()
{
var countdownEvent = new CountdownEvent(3);
var queue = bus.Advanced.QueueDeclare("multiple_types");
bus.Advanced.Consume(queue, x => x
.Add<MyMessage>((message, info) =>
{
Console.WriteLine("Got MyMessage {0}", message.Body.Text);
countdownEvent.Signal();
})
.Add<MyOtherMessage>((message, info) =>
{
Console.WriteLine("Got MyOtherMessage {0}", message.Body.Text);
countdownEvent.Signal();
})
.Add<IAnimal>((message, info) =>
{
Console.WriteLine("Got IAnimal of type {0}", message.Body.GetType().Name);
countdownEvent.Signal();
})
);
bus.Advanced.Publish(Exchange.GetDefault(), queue.Name, false, new Message<MyMessage>(new MyMessage { Text = "Hello" }));
bus.Advanced.Publish(Exchange.GetDefault(), queue.Name, false, new Message<MyOtherMessage>(new MyOtherMessage { Text = "Hi" }));
bus.Advanced.Publish(Exchange.GetDefault(), queue.Name, false, new Message<Dog>(new Dog()));
countdownEvent.Wait(1000);
}
示例15: Test
public void Test()
{
var CustomThreadPool = new CustomThreadPool(2);
var Results0 = new List<int>();
var Results1 = new List<int>();
var CountdownEvent = new CountdownEvent(2);
CustomThreadPool.AddTask(0, () =>
{
Thread.Sleep(10);
Results0.Add(0);
});
CustomThreadPool.AddTask(0, () =>
{
Results0.Add(1);
CountdownEvent.Signal();
});
CustomThreadPool.AddTask(1, () =>
{
Results1.Add(0);
CountdownEvent.Signal();
});
CountdownEvent.Wait();
Thread.Sleep(10);
Assert.IsTrue(CustomThreadPool.GetLoopIterCount(0) <= 2);
Assert.IsTrue(CustomThreadPool.GetLoopIterCount(1) <= 2);
Assert.AreEqual("0,1", Results0.ToStringArray());
Assert.AreEqual("0", Results1.ToStringArray());
CustomThreadPool.Stop();
}