C# 无法在Windows 7上以标准用户身份创建信号量
我不知道这是否是出于设计,但我似乎无法作为标准用户或超级用户在Windows7上创建新的信号量C# 无法在Windows 7上以标准用户身份创建信号量,c#,.net,windows-7,synchronization,semaphore,C#,.net,Windows 7,Synchronization,Semaphore,我不知道这是否是出于设计,但我似乎无法作为标准用户或超级用户在Windows7上创建新的信号量 SemaphoreSecurity semSec = new SemaphoreSecurity(); // have also tried "Power Users", "Everyone", etc. SemaphoreAccessRule rule = new SemaphoreAccessRule("Users", SemaphoreRights.FullControl, AccessC
SemaphoreSecurity semSec = new SemaphoreSecurity();
// have also tried "Power Users", "Everyone", etc.
SemaphoreAccessRule rule = new SemaphoreAccessRule("Users", SemaphoreRights.FullControl, AccessControlType.Allow);
semSec.AddAccessRule(rule);
bool createdNew = false;
// throws exception
sem = new Semaphore(1, 1, SEMAPHORE_ID, out createdNew, semSec);
return true;
我收到一个UnauthorizedAccessException
,消息是“访问端口被拒绝”
这可能吗?看看,解决方案似乎是为信号量创建设置安全级别
以下是从给定链接中摘录的源代码:
// The value of this variable is set by the semaphore
// constructor. It is true if the named system semaphore was
// created, and false if the named semaphore already existed.
//
bool semaphoreWasCreated;
// Create an access control list (ACL) that denies the
// current user the right to enter or release the
// semaphore, but allows the right to read and change
// security information for the semaphore.
//
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
SemaphoreSecurity semSec = new SemaphoreSecurity();
SemaphoreAccessRule rule = new SemaphoreAccessRule(
user,
SemaphoreRights.Synchronize | SemaphoreRights.Modify,
AccessControlType.Deny);
semSec.AddAccessRule(rule);
rule = new SemaphoreAccessRule(
user,
SemaphoreRights.ReadPermissions | SemaphoreRights.ChangePermissions,
AccessControlType.Allow);
semSec.AddAccessRule(rule);
// Create a Semaphore object that represents the system
// semaphore named by the constant 'semaphoreName', with
// maximum count three, initial count three, and the
// specified security access. The Boolean value that
// indicates creation of the underlying system object is
// placed in semaphoreWasCreated.
//
sem = new Semaphore(3, 3, semaphoreName,
out semaphoreWasCreated, semSec);
// If the named system semaphore was created, it can be
// used by the current instance of this program, even
// though the current user is denied access. The current
// program enters the semaphore. Otherwise, exit the
// program.
//
if (semaphoreWasCreated)
{
Console.WriteLine("Created the semaphore.");
}
else
{
Console.WriteLine("Unable to create the semaphore.");
return;
}
希望这有帮助 经过更多的研究和尝试,我终于解决了这个问题 关键是在信号量名称前加上“Global\”,即
const string NAME = "Global\\MySemaphore";
谢谢。在所有这些中,我相信只有互斥在进程间工作。互斥锁并没有我需要的信号方式——任何线程都可以对信号量调用release(),而对于互斥锁,只有获得锁的线程才能释放它,并且没有计数机制。