c# - 在 C# 中使用信号量

标签 c# multithreading visual-studio-2010 semaphore

您好,我正在尝试在我的应用程序中使用 Semaphore。我已经这样声明了。

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; 我能够使用 Mutex。 可能是我所缺少的。还有其他引用资料需要补充吗?我正在使用 VS 2010。框架4.0

最佳答案

信号量是 introduced in .NET 2.0

System.Threading.dll 是在 .NET 4.0 中添加的(尽管 namespace System.Threading 自 v1 以来就已存在)。也许您的项目包含旧版本的 System.Threading。

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

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

关于c# - 在 C# 中使用信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15658318/

相关文章:

java - 如果 wait() 抛出 InterruptedException,线程是否等待直到它获取对象的监视器?

java - 如何在这段代码中获取我的java线程的名称?

c++ - IDL与c++源文件的关系

.net-4.0 - Windows Workflow 4.0 事件未添加到工具箱

c# - 我需要一种方法将所有方法包装在 try catch 中

c# - 在 C# 中将列表对象转换为 json

c# - EF 核心中的自引用错误 : the insert statement conflicted with the foreign key same table constraint

c# - 申请.退出

java - 非线程安全 HashSet 上的并发添加 - 可能发生的最坏情况是什么?

vb.net - 在运行时向表单内的对象(即文本框)添加自定义属性