【问题标题】:Windows App Certification Kit fails: Multi user session testWindows 应用认证工具包失败:多用户会话测试
【发布时间】:2014-03-18 16:21:36
【问题描述】:

我正在尝试通过 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) 如何正确防止多个会话在同一个用户会话中运行,但允许多个实例跨多个用户会话?

2) 我可以在不同的用户会话中注册相同的热键吗?还是必须在切换用户会话时以某种方式取消注册并重新注册热键?

TIA

【问题讨论】:

  • 我认为使用互斥锁是防止多个实例的更好方法

标签: c# windows desktop-application app-certification-kit wack


【解决方案1】:

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

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

使用示例(来自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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多