是否可以完全阻止 .NET Compact Framework 上的多个实例?

发布于 2024-12-05 10:09:27 字数 647 浏览 3 评论 0原文

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

目前,我通过创建“互斥体”并检查是否有相同的进程正在运行来获得解决方案。我将此语句放在“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 的类。

Since I've try many ways to stop the multiple instance problem on handheld device which running on .net compact framework 3.5.

Currently , I got the solution by create "Mutex" and check if there is the same process is running. I put this statement in "Program.cs" which will executed at first time when program start.

But i think it's not shoot my problems cause I got request from user that they need to disable the "program icon" while it's running.

I understand the user's point that sometime they maybe "Open" the program multiple times or more within short period. So , If it still able to "Open". That mean the program will need to initial itself and maybe going fail finally. Is it possible to absolutely prevent the multiple instance ? or is there another way without programming like edit the registry on Windows CE ?


Here is my source code:

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

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

NamedMutex is class from OpenNetCF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-12-12 10:09:27

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

编辑:

这里有一些代码片段:

[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(包括评论)

什么是最好的方法在 Compact Framework 中制作单实例应用程序?

Your code is almost fine. only missing thing is to remove the application exit and put in there the code needed to bring current running instance on top. i did this in the past so you do not need to disable or hide the icon you simply detect the already running instance and bring it on foreground.

Edit:

here some code snippet:

[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;
}

check these links for more info...

http://www.nesser.org/blog/archives/56 (including comments)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文