⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace CrossProcess
{
   class Program
   {
      
      static void ThreadMain(object o)
      {
         Semaphore semaphore = o as Semaphore;
         Trace.Assert(semaphore != null, "o must be a Semaphore type");
         bool isCompleted = false;
         while (!isCompleted)
         {
            if (semaphore.WaitOne(600, false))
            {
               try
               {
                  Console.WriteLine("Thread {0} locks the sempahore",
                        Thread.CurrentThread.ManagedThreadId);
                  Thread.Sleep(2000);
               }
               finally
               {
                  semaphore.Release();
                  Console.WriteLine("Thread {0} releases the semaphore",
                     Thread.CurrentThread.ManagedThreadId);
                  isCompleted = true;
               }
            }
            else
            {
               Console.WriteLine("Timeout for thread {0}; wait again",
                  Thread.CurrentThread.ManagedThreadId);
            }
         }
      }

      static void Main(string[] args)
      {
         //bool createdNew;
         //Mutex mutex = new Mutex(false, "ThreadingDemo", out createdNew);
         //if (mutex.WaitOne())
         //{
         //   try
         //   {


         //   }
         //   finally
         //   {
         //      mutex.ReleaseMutex();
         //   }
         //}
         //else
         //{
         //   // some problem happened 
         //}

         int threadCount = 6;
         int semaphoreCount = 4;
         Semaphore semaphore = new Semaphore(semaphoreCount, semaphoreCount);
         Thread[] threads = new Thread[threadCount];

         for (int i = 0; i < threadCount; i++)
         {
            threads[i] = new Thread(ThreadMain);
            threads[i].Start(semaphore);
            
         }

         for (int i = 0; i < threadCount; i++)
         {
            threads[i].Join();
         }
         Console.WriteLine("all threads finished");
         
         
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -