• 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);
        }
  • 相关阅读:
    android入门教程(十六)之 使用Intent传递数据
    Android入门教程(十八)之ListView (二) (转)
    Android入门教程(十四)之ListView的应用 (转)
    Android入门教程(十七)之GridView(转自http://blog.csdn.net/hellogv/)
    (原创)Android入门教程(十五)之 Activity生命周期及其配置使用
    Android入门教程(十九)之ListView (三) (转)
    Android入门教程(十三)之自定义下拉菜单模式Spinner与setDropDownViewResource的应用(转)
    Android入门教程(十)之Menu功能菜单设计 (转)
    [vp]ARC059
    欧拉路学习笔记
  • 原文地址:https://www.cnblogs.com/xlfj521/p/883909.html
Copyright © 2020-2023  润新知