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);
        }
  • 相关阅读:
    springboot事物和事物回滚
    MyBatis Sql语句中的转义字符
    使用 vagrant新建Linux虚拟机
    Centos 6.7 安装mongodb
    阿里云windows server 2012 TIME_WAIT CLOSE_WAIT
    使用Eclipse打jar包 包含依赖jar包
    linux crontab定时器
    mysql 存储过程学习笔记
    oracle windows 新建用户授权 导出导入bmp文件
    解决hash冲突的方法
  • 原文地址:https://www.cnblogs.com/scgw/p/1563364.html
Copyright © 2011-2022 走看看