2013-03-27 58 views
3

嗨我想在我的应用程序中使用信号量。我已经这样声明。在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;我能够使用互斥。 可能是我错过了什么。还有其他需要添加的参考。?我正在使用VS 2010。框架4.0

回答

1

信号灯是另一个版本的introduced in .NET 2.0

System.Threading.dll加入在.NET 4.0中(虽然由于V1命名空间的System.Threading一直围绕)。也许你的项目包含一个旧版本的System.Threading。

这是一个经常被问到的问题The type or namespace '' does not exist in the class or namespace '' (are you missing an assembly reference?)

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

+1

It在'System.dll'中定义,显然他正在使用它。他还提到它是.NET框架4. – oleksii 2013-03-27 12:10:18

+1

@oleksii:不一定。 http://social.msdn.microsoft.com/Forums/en-US/netfxsetup/thread/545b1628-23fb-4d69-ab42-09ed008e3b46/ 和System.Threading.dll被添加到.NET 4.0中 – 2013-03-27 12:10:39

+0

我已添加参考'系统'现在再次工作 – Rakesh 2013-03-27 12:17:38

1
.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 

可能需要框架

0

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

0

此代码编译为我

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.dllV4.0.0.0,运行版本v4.0.30319,位于C: \ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.dll