当前位置: 代码网 > it编程>编程语言>Asp.net > 防止.NET应用多实例运行的有效方法

防止.NET应用多实例运行的有效方法

2025年02月13日 Asp.net 我要评论
核心思路1、检查是否已有程序实例运行使用 system.diagnostics.process 获取当前程序进程,并与系统中所有同名的程序进程进行比较。如果发现另一个同名进程,则认为程序已经运行。2、

核心思路

1、检查是否已有程序实例运行

使用 system.diagnostics.process 获取当前程序进程,并与系统中所有同名的程序进程进行比较。如果发现另一个同名进程,则认为程序已经运行。

2、激活已运行的程序窗口

如果发现已有实例运行,使用 win32 api 操作将其窗口置于前台,避免用户启动多个实例。

3、运行主程序逻辑

如果没有其他实例运行,正常启动主程序。

代码解析

1、检查是否已有程序运行

以下代码通过进程名称筛选当前运行的程序进程列表,返回除当前进程外的其他同名进程(如果存在):

public static process getexistprocess()  
{  
    process currentprocess = process.getcurrentprocess(); // 当前程序  
    foreach (process process in process.getprocessesbyname(currentprocess.processname))  
    {  
        if (process.id != currentprocess.id) // 找到其他同名进程  
        {  
            return process; // 返回已运行的进程  
        }  
    }  
    return null; // 没有其他实例  
}  

2、主程序入口

在 main 方法中调用 getexistprocess 检查程序实例:

static class program  
{  
    [stathread]  
    static void main()  
    {  
        application.enablevisualstyles();  
        application.setcompatibletextrenderingdefault(false);  

        var runningprocess = getexistprocess();  
        if (runningprocess != null) // 如果有实例运行  
        {  
            intptr mainwindowhandle = runningprocess.mainwindowhandle;  
            if (mainwindowhandle == intptr.zero) // 窗口被隐藏  
            {  
                mainwindowhandle = findwindow(null, "唯一程序");  
            }  
            showwindowasync(mainwindowhandle, 1); // 显示窗口  
            setforegroundwindow(mainwindowhandle); // 置于前台  
        }  
        else  
        {  
            application.run(new mainform()); // 启动新实例  
        }  
    }  
}  

3、win32 api 调用

以下是关键的 win32 api 函数,用于操作窗口显示和前台设置:

[dllimport("user32.dll", entrypoint = "findwindow")]  
public static extern intptr findwindow(string lpclassname, string lpwindowname);  

[dllimport("user32.dll")]  
public static extern bool showwindowasync(intptr hwnd, int ncmdshow);  

[dllimport("user32.dll ")]  
public static extern bool setforegroundwindow(intptr hwnd);  

findwindow: 根据窗口名称查找窗口句柄。

showwindowasync: 显示或隐藏窗口。

setforegroundwindow: 将窗口设置为前台窗口。

注意事项

1、窗口名称匹配问题

如果窗口标题动态变化,findwindow 的效果可能不佳,需保证窗口标题唯一或改用其他识别方法。 2、权限问题

如果以管理员权限运行程序,setforegroundwindow 操作可能会被限制。需要确保权限一致。

3、多线程问题

如果程序使用了多线程,需注意线程间的窗口操作同步。

优化建议

1、使用互斥量

利用 mutex 可以更简洁地实现程序唯一性。例如:

static mutex mutex = new mutex(true, "uniqueappname", out bool creatednew);  
    if (!creatednew)  
    {  
    // 已经有实例运行  
    return;  
    }  

2、窗口句柄缓存

可以在程序启动时缓存主窗口句柄,避免频繁调用 findwindow

3、日志记录

在实际应用中建议记录重复启动的尝试,以便后续调试和优化。

总结

这是一种基于进程和 win32 api 的方法来保持程序唯一性的解决方案。代码逻辑清晰,适用于大多数桌面应用场景。如果你有更复杂的需求或更高的性能要求,建议结合互斥量或现代框架特性来实现更可靠的方案。

最后

到此这篇关于防止.net应用多实例运行的有效方法的文章就介绍到这了,更多相关防止.net多实例运行内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com