c# - 是否有可能完全防止 .NET Compact Framework 上的多个实例?

标签 c# .net compact-framework multiple-instances handheld

因为我已经尝试了很多方法来阻止在 .net compact Framework 3.5 上运行的手持设备上的多实例问题。

目前,我通过创建“Mutex”获得了解决方案,并检查是否有相同的进程正在运行。我把这条语句放在“Program.cs”中,它会在程序启动时第一次执行。

但我认为这不是我的问题,因为我收到用户的请求,他们需要在运行时禁用“程序图标”。

我理解用户的观点,有时他们可能会在短时间内多次或多次“打开”程序。所以,如果它仍然能够“打开”。这意味着该程序将需要自行初始化,最终可能会失败。是否可以绝对防止多重实例?还是有另一种无需编程的方法,例如在 Windows CE 上编辑注册表?


这是我的源代码:

bool firstInstance;
NamedMutex mutex = new NamedMutex(false, "MyApp.exe", out firstInstance);

if (!firstInstance)
{
    //DialogResult dialogResult = MessageBox.Show("Process is already running...");
    Application.Exit();
}

NamedMutex 是来自 OpenNetCF 的类。

最佳答案

您的代码几乎没问题。唯一缺少的是删除应用程序导出并将当前正在运行的实例置于顶部所需的代码放入其中。我过去这样做过,所以您不需要禁用或隐藏图标,您只需检测已经运行的实例并将其置于前台即可。

编辑:

这里是一些代码片段:

[DllImport("coredll.dll")]
private static extern IntPtr FindWindow(IntPtr className, string windowName);

[DllImport("coredll.dll")]
internal static extern int SetForegroundWindow(IntPtr hWnd);

[DllImport("coredll.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, int hwnd2, int x,int y, int cx, int cy, int uFlags);

if (IsInstanceRunning())
{
    IntPtr h = FindWindow(IntPtr.Zero, "Form1");
    SetForegroundWindow(h);
    SetWindowPos(h, 0, 0, 0, Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height, 0x0040);

    return;
}

查看这些链接以获取更多信息...

http://www.nesser.org/blog/archives/56 (包括评论)

What is the best way to make a single instance application in Compact Framework?

关于c# - 是否有可能完全防止 .NET Compact Framework 上的多个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7468881/

相关文章:

.net - 如何在 PowerShell 中设置文化?

c# - 如何使用 Ctrl+v 在文本框中启用粘贴

c# - Windows Mobile - Attach on call 开始和录制通话

c# - 如何从 Windows Mobile 应用程序实现向导?

c# - 在哪里可以找到 Win32 常量?

c# - 链接资源中的图像 url

c# - 获取 C# 中的分配总数

c# .net core linq toEntity异步调用终止程序

c# - Linq to SQL 立即执行查询

C# 将 int 类型的 ArrayList 转换为 Point[]