2013-03-27 57 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