当前位置: 代码迷 >> WinCE >> Wince6.0 C# 应用程序只运行一次解决思路
  详细解决方案

Wince6.0 C# 应用程序只运行一次解决思路

热度:173   发布时间:2016-04-28 12:00:45.0
Wince6.0 C# 应用程序只运行一次
我用过下面的方法,但不行,GetLastError返回87,参数不正确。

            private const int ERROR_ALREADY_EXISTS = 183;
           
            string strAppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            IntPtr hMutex = CreateMutex(null, false, strAppName);
            int lasterror = GetLastError();
            if (lasterror != ERROR_ALREADY_EXISTS)
             {
                  。。。。
             }

             请教各位Wince6.0 C# 应用程序只运行一次怎么做?
------解决方案--------------------
C# 没有用过

用互斥量的办法肯定是可以的
也可以参考使用 FindWindow 来找窗口,如果找到就退出。
------解决方案--------------------
  #region OpenNETCF native interface to mutex generation (version 1.4 of the SDF)

        public const Int32 NATIVE_ERROR_ALREADY_EXISTS = 183;

        #region P/Invoke Commands for Mutexes

        [DllImport("coredll.dll", EntryPoint = "CreateMutex", SetLastError = true)]
        public static extern IntPtr CreateMutex(
            IntPtr lpMutexAttributes,
            bool InitialOwner,
            string MutexName);

        [DllImport("coredll.dll", EntryPoint = "ReleaseMutex", SetLastError = true)]
        public static extern bool ReleaseMutex(IntPtr hMutex);

        #endregion

        public static bool IsInstanceRunning()
        {
            IntPtr hMutex = CreateMutex(IntPtr.Zero, true, "wmspda");
            if (hMutex == IntPtr.Zero)
            {
                throw new ApplicationException("Failure creating mutex: " + Marshal.GetLastWin32Error().ToString("X"));
            }
            if (Marshal.GetLastWin32Error() == NATIVE_ERROR_ALREADY_EXISTS)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
      
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        static void Main()
        {
            try
            {

                if (!IsInstanceRunning())
                {
                    Application.Run(new Home());
                }
                else 
                {
                    MessageBox.Show("程序已经在运行!");
  相关解决方案