在C#中使用信号量 [英] using semaphore in c#

查看:372
本文介绍了在C#中使用信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序中使用信号量.我已经这样声明了.

Hi I am trying to use Semaphore in my application.I have declared like this.

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();
 }

但是我不能编译它给我这个错误

But I am not able to compile Its giving me this error

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 . Framework 4.0

But I have already added the namespace using System.Threading; I am able to using Mutex. May be what was I am missing. There is any other reference need to add.? I am using VS 2010. Framework 4.0

推荐答案

信号量为.NET 2.0中引入的

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

System.Threading.dll was added in .NET 4.0 (though the namespace System.Threading has been around since v1). Maybe your project includes an older version of System.Threading.

这是一个常见问题

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

You need to add a reference in your project to an assembly where that namespace is defined or you are missing an using statement despite you stating that you are using System.Threading.

这篇关于在C#中使用信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆