zoukankan      html  css  js  c++  java
  • C#程序多用户只启动一个进程的方法

    Main函数

    [STAThread]
            
    static void Main()
            
    {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);
                webscreenshot wb 
    = new webscreenshot();
                Process current 
    = Process.GetCurrentProcess();
                
    bool newinstance = true;
                Process[] processes 
    = Process.GetProcessesByName(current.ProcessName);

                
    //遍历正在有相同名字运行的例程  
                foreach (Process process in processes)
                
    {
                    
    //忽略现有的例程  
                    if (process.Id != current.Id)
                    
    {
                        
    //确保例程从EXE文件运行  
                        if (Assembly.GetExecutingAssembly().Location.Replace("/""\\"== current.MainModule.FileName)
                        
    {
                            
    //返回另一个例程实例  
                            current = process;
                            newinstance 
    = false;
                            
    break;
                        }

                    }

                }

                
    if (newinstance)
                
    {
                    Application.Run(wb);
                }

                
    else
                
    {
                    ShowWindowAsync(current.MainWindowHandle, 
    1);

                    
    //设置真实例程为foreground   window  
                    SetForegroundWindow(current.MainWindowHandle);
                }

            }



    引入这两个API函数

    [DllImport("User32.dll")]
            
    private static extern bool ShowWindowAsync(  IntPtr hWnd, int cmdShow);

            [DllImport(
    "User32.dll")]
            
    private static extern bool SetForegroundWindow(IntPtr hWnd);
        }
  • 相关阅读:
    十步完全理解SQL
    c#退出应用程序办法
    几个有意思的算法题
    GeoServer不同服务器安装配置、数据发布及客户端访问
    开启httpd服务的时候 显示Could not reliably determine the server`s fully qualified domain name
    Working With OpenLayers(Section 1: Creating a Basic Map)
    GeoServer地图开发解决方案(五):基于Silverlight技术的地图客户端实现
    模拟远程HTTP的POST请求
    模拟提交带附件的表单
    支付宝手机网站接口对接
  • 原文地址:https://www.cnblogs.com/xlfj521/p/883909.html
Copyright © 2011-2022 走看看