zoukankan      html  css  js  c++  java
  • WPF 同一个程序 只允许 同时运行一个

     
     
     
     

    方法2  当程序已经运行了 再运行这个程序时,则显示当前这个窗体  http://code.3rbang.com/cshape-run-one/
     
     
    .NET 已经提供一个 类 System.Threading.Mutex 所以不采用API CreateMutex
     App.xaml.cs 中实现代码,重这写 OnStartup
     
     
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Windows;
     
    namespace WpfApplication7
    {
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
            #region DllImport...
     
            [System.Runtime.InteropServices.DllImport("User32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
     
     
            [System.Runtime.InteropServices.DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWndint cmdShow);
     
            private const int SW_SHOW = 1;
            #endregion
     
     
     
     
            bool createdNew;
            protected override void OnStartup(StartupEventArgs e)
            {
                System.Threading.Mutex mutex = new System.Threading.Mutex(true"HelloRoman"out createdNew);
                if (!createdNew)
                {
                    System.Diagnostics.Process progress1 = GetExistProcess();
                    if (progress1 != null)
                    {
                        ShowMainWindow(progress1);
                        Environment.Exit(0);
                    }
     
                }
            }
     
     
            /// <summary>
            /// 最前端显示主窗体
            /// </summary>
            /// <param name="process"></param>
            private void ShowMainWindow(System.Diagnostics.Process process)
            {
                IntPtr mainWindowHandle1 = process.MainWindowHandle;
                if (mainWindowHandle1 != IntPtr.Zero)
                {
                    ShowWindowAsync(mainWindowHandle1, SW_SHOW);
                    SetForegroundWindow(mainWindowHandle1);
                }
            }
     
            /// <summary>
            /// 查看程序是否已经运行
            /// </summary>
            /// <returns></returns>
            private static System.Diagnostics.Process GetExistProcess()
            {
                System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
                foreach (System.Diagnostics.Process process1 in System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName))
                {
                    if ((process1.Id != currentProcess.Id) &&
                         (System.Reflection.Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
                    {
                        return process1;
                    }
                }
                return null;
            }
     
     
        }
    }

    方法1
     
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
     
     
    namespace WpfApplication6
    {
     
     
     
     
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
     
            [System.Runtime.InteropServices.DllImport("kernel32.dll"EntryPoint = "CreateMutex")]
            public static extern IntPtr CreateMutex(int lpSecurityAttributesbool bInitialOwnerstring lpName);
     
            [System.Runtime.InteropServices.DllImport("kernel32.dll"EntryPoint = "GetLastError")]
            public static extern int GetLastError();
            private const int ERROR_ALREADY_EXISTS = 183;
     
     
     
     
            protected override void OnStartup(StartupEventArgs e)
            {
                string appTitle = "2014年5月31日7:28:29";
                IntPtr hMutex = CreateMutex(0, trueappTitle);
                if (GetLastError() == ERROR_ALREADY_EXISTS)
                {
                    MessageBox.Show("程序已经运行!");
                    Application.Current.Shutdown();
                }
            }
     
        }
    }
     





  • 相关阅读:
    UVa Live 3942 Remember the Word
    UVa 11019 Matrix Matcher
    bzoj 4445 小凸想跑步
    Codeforces 832E Vasya and Shifts
    二值法方法综述及matlab程序
    排序算法(1)-插入,选择,冒泡
    如果我想了解一个陌生人
    Libsvm在matlab环境下使用指南
    科学预测世界杯-采用机器学习方法
    应用笔画宽度变换(SWT)来检测自然场景中的文本
  • 原文地址:https://www.cnblogs.com/xe2011/p/3762064.html
Copyright © 2011-2022 走看看