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);
        }
  • 相关阅读:
    深入理解分布式事务,高并发下分布式事务的解决方案
    java分布式事务,及解决方案
    java的两种同步方式, Synchronized与ReentrantLock的区别
    MYSQL 查看最大连接数和修改最大连接数
    SpringCloud学习:Eureka、Ribbon和Feign
    dubbo支持协议及具体对比
    如何正确地给图像添加高斯噪声
    图像质量评价指标之 PSNR 和 SSIM
    超光谱图像去噪基准
    LeetCode 240——搜索二维矩阵 II
  • 原文地址:https://www.cnblogs.com/xlfj521/p/883909.html
Copyright © 2011-2022 走看看