本文整理汇总了C#中AutoResetEvent.WaitOne方法的典型用法代码示例。如果您正苦于以下问题:C# AutoResetEvent.WaitOne方法的具体用法?C# AutoResetEvent.WaitOne怎么用?C# AutoResetEvent.WaitOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AutoResetEvent
的用法示例。
在下文中一共展示了AutoResetEvent.WaitOne方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public int Run()
{
AutoResetEvent are = new AutoResetEvent(true);
Stopwatch sw = new Stopwatch();
if(!are.WaitOne(0))//,false))
{
Console.WriteLine("Signalled event should always return true on call to !are.WaitOne(0,false).");
return -3;
}
sw.Start();
bool ret = are.WaitOne(1000);//,false);
sw.Stop();
//We should never get signaled
if(ret)
{
Console.WriteLine("AutoResetEvent should never be signalled.");
return -1;
}
if(sw.ElapsedMilliseconds < 900)
{
Console.WriteLine("It should take at least 900 milliseconds to call bool ret = are.WaitOne(1000,false);.");
Console.WriteLine("sw.ElapsedMilliseconds = " + sw.ElapsedMilliseconds);
return -2;
}
return 100;
}
示例2: Main
static void Main()
{
// Create an event to signal the timeout count threshold in the
// timer callback.
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create an inferred delegate that invokes methods for the timer.
TimerCallback tcb = statusChecker.CheckStatus;
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
示例3: Main
public static void Main()
{
AutoResetEvent auto = new AutoResetEvent(true);
bool state = auto.WaitOne(5000, true);
Console.WriteLine("After Frist Wait : " + state);
state = auto.WaitOne(10000, true);
Console.WriteLine("After Second Wait : " + state);
}
示例4: Main
static int Main ()
{
SoapServerFormatterSinkProvider serverFormatter = new SoapServerFormatterSinkProvider ();
serverFormatter.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable ht = new Hashtable ();
ht ["name"] = "hydraplus";
ht ["port"] = 8081;
ht ["authorizedGroup"] = "everyone";
HttpChannel channel = new HttpChannel (ht, null, serverFormatter);
#if NET_2_0
ChannelServices.RegisterChannel (channel, false);
#else
ChannelServices.RegisterChannel (channel);
#endif
WellKnownServiceTypeEntry entry = new WellKnownServiceTypeEntry (
typeof (ServerTalk), "hydraplus.soap",
WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType (entry);
ServerTalk.NewUser = new delUserInfo (NewClient);
ServerTalk.DelUser = new delRemoveUser (RemoveClient);
ServerTalk.ClientToHost = new delCommsInfo (ClientToHost);
clientConnected = new AutoResetEvent (false);
clientDisconnected = new AutoResetEvent (false);
// wait for client to connect
if (!clientConnected.WaitOne (20000, false)) {
ReportFailure ("No client connected in time.");
return 1;
}
// wait for message to arrive
while (true) {
Thread.Sleep (50);
if (ServerTalk.ClientToServerQueue.Count > 0) {
CommsInfo info = (CommsInfo) ServerTalk.ClientToServerQueue.Dequeue ();
ClientToHost (info);
break;
}
}
// send message to client
ServerTalk.RaiseHostToClient (client.Id, "txt", receivedMsg);
// wait for client to disconnect
if (clientConnected.WaitOne (2000, false)) {
ReportFailure ("Client did not disconnect in time.");
return 2;
}
return 0;
}
示例5: NonRepeatingTimer_ThatHasAlreadyFired_CanChangeAndFireAgain
public void NonRepeatingTimer_ThatHasAlreadyFired_CanChangeAndFireAgain()
{
AutoResetEvent are = new AutoResetEvent(false);
TimerCallback tc = new TimerCallback((object o) => are.Set());
using (var t = new Timer(tc, null, 1, Timeout.Infinite))
{
Assert.True(are.WaitOne(MaxPositiveTimeoutInMs), "Should have received first timer event");
Assert.False(are.WaitOne(500), "Should not have received a second timer event");
t.Change(10, Timeout.Infinite);
Assert.True(are.WaitOne(MaxPositiveTimeoutInMs), "Should have received a second timer event after changing it");
}
}
示例6: SetReset
public void SetReset()
{
using (AutoResetEvent are = new AutoResetEvent(false))
{
Assert.False(are.WaitOne(0));
are.Set();
Assert.True(are.WaitOne(0));
Assert.False(are.WaitOne(0));
are.Set();
are.Reset();
Assert.False(are.WaitOne(0));
}
}
示例7: Timer_Change_BeforeDueTime_ChangesWhenTimerWillFire
public void Timer_Change_BeforeDueTime_ChangesWhenTimerWillFire()
{
AutoResetEvent are = new AutoResetEvent(false);
using (var t = new Timer(new TimerCallback((object s) =>
{
are.Set();
}), null, TimeSpan.FromSeconds(500), TimeSpan.FromMilliseconds(50)))
{
Assert.False(are.WaitOne(TimeSpan.FromMilliseconds(100)), "The reset event should not have been set yet");
t.Change(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(-1));
Assert.True(are.WaitOne(TimeSpan.FromMilliseconds(TimerFiringTests.MaxPositiveTimeoutInMs)), "Should have received a timer event after this new duration");
}
}
示例8: PosTest1
public bool PosTest1()
{
bool retVal = true;
AutoResetEvent are;
TestLibrary.TestFramework.BeginScenario("PosTest1: AutoResetEvent.Set()");
try
{
// false means that the initial state should be not signaled
are = new AutoResetEvent(false);
// set the signal
if (!are.Set())
{
TestLibrary.TestFramework.LogError("001", "AutoResetEvent.Set() failed");
retVal = false;
}
// verify that the autoreset event is signaled
// if it is not signaled the following call will block for ever
TestLibrary.TestFramework.LogInformation("Calling AutoResetEvent.WaitOne()... if the event is not signaled it will hang");
are.WaitOne();
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例9: Main
static int Main ()
{
receivedMsg = new AutoResetEvent (false);
HttpChannel channel = new HttpChannel (0);
#if NET_2_0
ChannelServices.RegisterChannel (channel, false);
#else
ChannelServices.RegisterChannel (channel);
#endif
ServerTalk _ServerTalk = (ServerTalk) Activator.GetObject (typeof (ServerTalk), "http://localhost:8081/hydraplus.soap");
CallbackSink _CallbackSink = new CallbackSink ();
_CallbackSink.OnHostToClient += new delCommsInfo (CallbackSink_OnHostToClient);
_ServerTalk.RegisterHostToClient ("Me", 0, new delCommsInfo (_CallbackSink.HandleToClient));
string messageSent = Guid.NewGuid ().ToString ();
_ServerTalk.SendMessageToServer (new CommsInfo ("Me", "txt", messageSent));
if (receivedMsg.WaitOne (5000, false)) {
Assert.AreEqual (messageReceived, messageSent, "#1");
return 0;
} else {
return 1;
}
}
示例10: Ctor
public void Ctor()
{
using (AutoResetEvent are = new AutoResetEvent(false))
Assert.False(are.WaitOne(0));
using (AutoResetEvent are = new AutoResetEvent(true))
Assert.True(are.WaitOne(0));
}
示例11: HandleCharacter
private static void HandleCharacter(Match match, HttpListenerResponse response)
{
// here we are running in a thread different from the main thread
int pid = Convert.ToInt32(match.Groups[1].Value);
string responseString = "";
// event used to wait the answer from the main thread.
AutoResetEvent autoEvent = new AutoResetEvent(false);
// var to store the character we are looking for
Character character = null;
// this bool is to check character is valid ... explanation below
bool found = false;
// we queue an 'action' to be executed in the main thread
MainGameObject.QueueOnMainThread(()=>{
// here we are in the main thread (see Update() in MainGameObject.cs)
// retrieve the character
character = MainGameObject.Instance().CharacterByID(pid);
// if the character is null set found to false
// have to do this because cannot call "character==null" elsewhere than the main thread
// do not know why (yet?)
// so if found this "trick"
found = (character!=null?true:false);
// set the event to "unlock" the thread
autoEvent.Set();
});
// wait for the end of the 'action' executed in the main thread
autoEvent.WaitOne();
// generate the HTTP answer
if (found==false) {
responseString = "<html><body>character: not found (" + pid + ")</body></html>";
} else {
responseString = "<html><body>";
responseString += "<img src='data:image/jpg;base64," + character.imageB64 + "'></img></br>";
responseString += "name: " + character.name + "</br>";
responseString += "life: " + character.life + "</br>";
responseString += "streght " + character.streght + "</br>";
responseString += "dexterity " + character.dexterity + "</br>";
responseString += "consitution " + character.consitution + "</br>";
responseString += "intelligence " + character.intelligence + "</br>";
responseString += "</body></html>";
}
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer,0,buffer.Length);
// You must close the output stream.
output.Close();
}
示例12: Running_Timer_CanBeFinalizedAndStopsFiring
public void Running_Timer_CanBeFinalizedAndStopsFiring()
{
AutoResetEvent are = new AutoResetEvent(false);
TimerCallback tc = new TimerCallback((object o) => are.Set());
var t = new Timer(tc, null, 1, 500);
Assert.True(are.WaitOne(MaxPositiveTimeoutInMs), "Failed to get first timer fire");
t = null; // Remove our refence so the timer can be GC'd
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.False(are.WaitOne(500), "Should not have received a timer fire after it was collected");
}
示例13: Timer_Fires_After_DueTime_Ellapses
public void Timer_Fires_After_DueTime_Ellapses()
{
AutoResetEvent are = new AutoResetEvent(false);
using (var t = new Timer(new TimerCallback((object s) =>
{
are.Set();
}), null, TimeSpan.FromMilliseconds(250), TimeSpan.FromMilliseconds(Timeout.Infinite) /* not relevant */))
{
Assert.True(are.WaitOne(TimeSpan.FromMilliseconds(MaxPositiveTimeoutInMs)));
}
}
示例14: WorkItemMethod
public static void WorkItemMethod(object mainEvent)
{
Console.WriteLine("\nStarting WorkItem.\n");
AutoResetEvent autoEvent = new AutoResetEvent(false);
// Create some data.
const int ArraySize = 10000;
const int BufferSize = 1000;
byte[] byteArray = new Byte[ArraySize];
new Random().NextBytes(byteArray);
// Create two files and two State objects.
FileStream fileWriter1 =
new FileStream(@"C:\[email protected]##.dat", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite,
BufferSize, true);
FileStream fileWriter2 =
new FileStream(@"C:\[email protected]##.dat", FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite,
BufferSize, true);
State stateInfo1 = new State(fileWriter1, autoEvent);
State stateInfo2 = new State(fileWriter2, autoEvent);
// Asynchronously write to the files.
fileWriter1.BeginWrite(byteArray, 0, byteArray.Length,
new AsyncCallback(EndWriteCallback), stateInfo1);
fileWriter2.BeginWrite(byteArray, 0, byteArray.Length,
new AsyncCallback(EndWriteCallback), stateInfo2);
// Wait for the callbacks to signal.
autoEvent.WaitOne();
autoEvent.WaitOne();
fileWriter1.Close();
fileWriter2.Close();
Console.WriteLine("\nEnding WorkItem.\n");
// Signal Main that the work item is finished.
((AutoResetEvent)mainEvent).Set();
}
示例15: TestWaitOneSingleThreaded
public void TestWaitOneSingleThreaded()
{
bool x;
e1 = new AutoResetEvent(false);
x = e1.WaitOne(10,false);
AssertEquals("WaitOne(unset)", x, false);
e1.Set();
x = e1.WaitOne(10,false);
AssertEquals("WaitOne(set)", x, true);
// It should be reset now.
x = e1.WaitOne(10,false);
AssertEquals("WaitOne(set)", x, false);
}