zoukankan      html  css  js  c++  java
  • [C#] 防止窗口重复打开

    来源:网络

     修改Program.cs文件

    using System;
    using System.Collections.Generic;
    using
    System.Linq;
    using
    System.Windows.Forms;

    namespace
    Test
    {
    static class
    Program
    {
    /// <summary>

    /// 应用程序的主入口点。
    /// </summary>

    [STAThread]
    static void Main(string
    [] args)
    {
    bool
    is_createdNew1;
    bool
    is_createdNew2;
    System.Threading.Mutex mu1
    = null
    ;
    System.Threading.Mutex mu2
    = null
    ;

    try

    {
    #region 檢查程式是否重複執行

    // 第一關:在同目錄執行相同程式的情況下不允許重複執行
    string mutexName1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(System.IO.Path.DirectorySeparatorChar, '_');
    mu1
    = new System.Threading.Mutex(true, "Global\\" + mutexName1, out
    is_createdNew1);
    if (!
    is_createdNew1)
    {
    return
    ;
    }

    // 第二關:在完全相同的傳入參數下不允許重複執行,避免數據重複計算

    string mutexName2 = "Args_" + String.Join("_", args).Replace(System.IO.Path.DirectorySeparatorChar, '_');
    mu2
    = new System.Threading.Mutex(true, "Global\\" + mutexName2, out
    is_createdNew2);
    if (!
    is_createdNew2)
    {
    return
    ;
    }

    #endregion


    //DoSomething();
    }
    catch
    (Exception ex)
    {
    throw
    ex;
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(
    false
    );
    Application.Run(
    new MainWindow()); //MainWindow是窗口名

    }
    }
    }
  • 相关阅读:
    BZOJ 2064: 分裂( 状压dp )
    BZOJ 2096: [Poi2010]Pilots( set )
    BZOJ 3444: 最后的晚餐( )
    BZOJ 3156: 防御准备( dp + 斜率优化 )
    BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )
    BZOJ 2466: [中山市选2009]树( 高斯消元 )
    BZOJ 1316: 树上的询问( 点分治 + 平衡树 )
    codevs 1074
    bzoj 1015
    bzoj 1798
  • 原文地址:https://www.cnblogs.com/hcbin/p/1687122.html
Copyright © 2011-2022 走看看