如何利用C#.NET达到同步.docVIP

  • 0
  • 0
  • 约3.7千字
  • 约 3页
  • 2018-05-26 发布于河南
  • 举报
如何利用C#.NET达到同步

a ReaderWriterLock object to control the access. To do this, follow these steps: Start Microsoft Visual Studio. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click Console Application under Templates. Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types, and then click Console Application under Templates. In the Name text box, type MultiThreadApplication, and then click OK. Replace the existing code with the following code in Class1.cs. Note In Visual Studio 2005 and in Visual Studio 2008, the default file is Program.cs. class Class1 { private ReaderWriterLock rwl = new ReaderWriterLock(); private long myNumber; public long Number // the Number property { get { //Acquire a read lock on the resource. rwl.AcquireReaderLock(Timeout.Infinite); try { Console.WriteLine(Thread:{0} starts getting the Number, Thread.CurrentThread.GetHashCode()); Thread.Sleep(50); Console.WriteLine(Thread:{0} got the Number, Thread.CurrentThread.GetHashCode()); } finally { //Release the lock. rwl.ReleaseReaderLock(); } return myNumber; } set { //Acquire a write lock on the resource. rwl.AcquireWriterLock(Timeout.Infinite); try { Console.WriteLine(Thread: {0} start writing the Number, Thread.CurrentThread.GetHashCode()); Thread.Sleep(50); myNumber = value; Console.WriteLine(Thread: {

文档评论(0)

1亿VIP精品文档

相关文档