本文整理匯總了C#中System.Threading.Monitor.Pulse方法的典型用法代碼示例。如果您正苦於以下問題:C# Monitor.Pulse方法的具體用法?C# Monitor.Pulse怎麽用?C# Monitor.Pulse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Monitor
的用法示例。
在下文中一共展示了Monitor.Pulse方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Reader
//引入命名空間
using System;
using System.Threading;
class MessageBoard {
private String messages = "no messages";
public void Reader() {
try {
Monitor.Enter(this);
if (messages == "no messages") {
Console.WriteLine("{0} {1}",Thread.CurrentThread.Name, messages);
Console.WriteLine("{0} waiting",Thread.CurrentThread.Name);
Monitor.Wait(this);
}
Console.WriteLine("{0} {1}",Thread.CurrentThread.Name, messages);
} finally {
Monitor.Exit(this);
}
}
public void Writer() {
try {
Monitor.Enter(this);
messages = "Greetings!";
Console.WriteLine("{0} Done writing message",Thread.CurrentThread.Name);
Monitor.Pulse(this);
} finally {
Monitor.Exit(this);
}
}
public static void Main() {
MessageBoard myMessageBoard = new MessageBoard();
Thread reader = new Thread(new ThreadStart(myMessageBoard.Reader));
reader.Name = "ReaderThread:";
Thread writer = new Thread(new ThreadStart(myMessageBoard.Writer));
writer.Name = "WriterThread:";
reader.Start();
writer.Start();
}
}