gpt4 book ai didi

c# - EventWaitHandle "No handle of the given name exists"Vagrant 远程虚拟机 C# PowerShell

转载 作者:行者123 更新时间:2023-11-30 23:23:40 27 4
gpt4 key购买 nike

我有一些代码在 Vagrant Hyper-V 虚拟机上运行,​​该虚拟机在启动之前一直等待直到它被联系。我试图告诉 EventWaitHandler 继续通过远程 PowerShell session 。等待的代码是C#,激活的代码是PowerShell。这是等待的代码:

var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow);
var security = new EventWaitHandleSecurity();
security.AddAccessRule(rule);
bool created;

var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "EventName", out created, security);
Console.WriteLine("Waiting to be started...");

wh.WaitOne();

我遇到的问题是,当我尝试通过远程 powershell session 获取 eventwaithandler 时,它告诉我它不存在。这是获取 waithandler 的 powershell 代码:

Enter-PSSession -ComputerName MyVm
# This works unless it is run in a remote session
$Mutex = [System.Threading.EventWaitHandle]::OpenExisting("EventName")

当我在远程 session 上运行此代码时,出现以下错误:

Exception calling "OpenExisting" with "1" argument(s): "No handle of the given name exists."

然后它给了我失败的代码,它指向上面的“OpenExisting”行。

只是想补充一点,它在同一台机器上都可以正常工作,所以我知道它应该 可以工作,但不能通过远程 session 。

如有任何帮助/指点,我们将不胜感激。

谢谢

最佳答案

找到答案,想把它贴在这里:

我唯一需要做的就是在进程名称前面加上“global\”像这样初始化(C#):

var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "Global\\EventName", out created, security);

然后像这样通过 PowerShell 访问它:

$Mutex = [System.Threading.EventWaitHandle]::OpenExisting("Global\EventName")

即使在远程 session 中也能像魅力一样工作......

关于c# - EventWaitHandle "No handle of the given name exists"Vagrant 远程虚拟机 C# PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com