gpt4 book ai didi

c# - Windows 应用程序认证工具包失败 : Multi user session test

转载 作者:可可西里 更新时间:2023-11-01 11:53:48 25 4
gpt4 key购买 nike

我试图通过 Windows 应用程序认证工具包提取 WinForms 应用程序,但在此测试中失败了:

  <TEST INDEX="17" NAME="Multi user session test" DESCRIPTION="Multi User checks Application invocation in multiple sessions." EXECUTIONTIME="00h:00m:24s.22ms">
<RESULT><![CDATA[FAIL]]></RESULT>
<MESSAGES />

我猜这是因为我只允许应用程序的一个实例运行,如下所示:

using ( var p = System.Diagnostics.Process.GetCurrentProcess() )
if ( System.Diagnostics.Process.GetProcessesByName( p.ProcessName ).Length > 1 )
{
MessageBox.Show(
"An instance of xxx is already running!",
Title,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation );
return;
}

这是一个由热键组合激活的托盘应用程序,注册了这个功能:

[DllImport( "user32", EntryPoint = "RegisterHotKey", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true )]
public static extern int RegisterHotkey( IntPtr hWnd, int id, int fsModifiers, int vk );

所以我想我有两个问题:

1) 如何正确地防止在同一用户 session 中运行多个 session ,但允许跨多个用户 session 运行多个实例?

2) 我可以在不同的用户 session 中注册相同的热键吗?或者当用户 session 切换时,我必须以某种方式注销并重新注册热键吗?

TIA

最佳答案

您可以使用 Mutex 实现相同的效果.有关详细信息,请参阅 MSDN,但简短的版本是使用以 "Local\" 开头的名称创建的任何互斥量都将是每个 session 。输入一个名为 "Local\MyAppName" 的互斥锁,每个 session 只能运行一个应用程序实例。

热键是按 session 注册的,在多个 session 中注册相同的热键不会有问题。

使用示例(来自 Run single instance of an application using Mutex):

bool ownsMutex = false;
// NOTE: Local is the default namespace, so the name could be shortened to myApp
Mutex myMutex = new Mutex(false, @"Local\myApp");

try
{
ownsMutex = myMutex.WaitOne(0)
}
catch (AbandonedMutexException)
{
ownsMutex = true;
}

if (!ownsMutex)
{
MessageBox.Show("myApp is already running!", "Multiple Instances");
return;
}
else
{
try
{
Application.Run(new Form1());
}
finally
{
myMutex.ReleaseMutex();
}
}

关于c# - Windows 应用程序认证工具包失败 : Multi user session test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485174/

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