在c#中使用信号量

嗨,我想在我的应用程序中使用信号量。我已经这样声明了。

using System; using System.Threading; // This thread allows only two instances of itself // to run at any one time. class MyThread { public Thread Thrd; static Semaphore sem = new Semaphore(2, 2); public MyThread(string name) { Thrd = new Thread(this.Run); Thrd.Name = name; Thrd.Start(); } 

但我无法编译它给我这个错误

 The type or namespace name 'Semaphore' could not be found (are you missing a using directive or an assembly reference?) 

但我已经using System.Threading;添加了命名空间using System.Threading; 我可以使用Mutex 。 可能是我所缺少的。 还有其他任何参考需要添加。 我正在使用VS 2010 。 框架4.0

信号量是在.NET 2.0中引入的

System.Threading.dll是在.NET 4.0中添加的(虽然命名空间System.Threading从v1开始就已存在)。 也许您的项目包含较旧版本的System.Threading。

这是一个经常被问到的问题类或命名空间”在类或命名空间中不存在”(你是否缺少程序集引用?)

您需要在项目中将引用添加到已定义该命名空间的程序集中,或者您缺少using语句,尽管您声明正在使用System.Threading。

  .NET Framework Supported in: 4.5, 4, 3.5, 3.0, 2.0 .NET Framework Client Profile Supported in: 4, 3.5 SP1 Portable Class Library Supported in: Portable Class Library .NET for Windows Store apps Supported in: Windows 8 

您可能需要其他版本的框架

如果添加4.0版本以获得Semaphore类的适当引用,则会更好。

这段代码为我编译

 using System.Threading; namespace ConsoleApp1 { class MyThread { static Semaphore sem = new Semaphore(2, 2); } class Program { static void Main(string[] args) { } } } 
  • VS 2010
  • .NET 4.0
  • 单引用System.dll v4.0.0.0,运行时版本v4.0.30319,位于C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.dll