using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace WpfApp5 { class App : Application { Mutex mutex; protected override void OnStartup(StartupEventArgs e) { string mutexName = "myApp"; bool createNew; mutex = new Mutex(true, mutexName, out createNew); if (!createNew) { MessageBox.Show("应用已经存在一个进程实例!"); this.Shutdown(); } else { base.OnStartup(e); } } } class Program { [STAThread] public static void Main(string[] args) { Application app = new App(); Window1 window1 = new Window1(); window1.Show(); //app.Run(); //下面的代码等同于上面的 Application.Current.Run(); } } }