zoukankan      html  css  js  c++  java
  • WinCe 如何使应用程序只开启一个

    方法一:

    namespace MyNameSpace
    {
    static class Program { [DllImport("Toolhelp.dll")] public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid); [DllImport("Toolhelp.dll")] public static extern int Process32First(IntPtr handle, ref PROCESSENTRY32 pe); [DllImport("Toolhelp.dll")] public static extern int Process32Next(IntPtr handle, ref PROCESSENTRY32 pe); /// <summary> /// 应用程序的主入口点。 /// </summary> [MTAThread] static void Main() { IntPtr handle = CreateToolhelp32Snapshot((uint)SnapShotFlags.TH32CS_SNAPPROCESS, 0); int count = 0; if ((int)handle != -1) { PROCESSENTRY32 pe32 = new PROCESSENTRY32(); pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)); int bMore = Process32First(handle, ref pe32); while (bMore == 1) { if (pe32.szExeFile == "自己的程序名") count++; bMore = Process32Next(handle, ref pe32); } } if (count > 1) { MessageBox.Show("程序已经运行!"); return; } else Application.Run(new FrmLogin()); } } [StructLayout(LayoutKind.Sequential)] public struct PROCESSENTRY32 { public uint dwSize; public uint cntUsage; public uint th32ProcessID; public IntPtr th32DefaultHeapID; public uint th32ModuleID; public uint cntThreads; public uint th32ParentProcessID; public int pcPriClassBase; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]//注意,此处为宽字符 public string szExeFile; public uint th32MemoryBase; public uint th32AccessKey; } public enum SnapShotFlags : uint { TH32CS_SNAPHEAPLIST = 0x00000001, TH32CS_SNAPPROCESS = 0x00000002, TH32CS_SNAPTHREAD = 0x00000004, TH32CS_SNAPMODULE = 0x00000008, TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE), TH32CS_GETALLMODS = 0x80000000 } }


    方法二:

    采用第三方的类库:

    OpenNETCF

    官方网址:http://www.opennetcf.com

    using OpenNETCF.Threading;
    
    namespace MyNameSpace
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [MTAThread]
            static void Main()
            {
                bool firstInstance;
                OpenNETCF.Threading.NamedMutex mutex = new OpenNETCF.Threading.NamedMutex(false, "自己起个名字", out firstInstance);
                if (!firstInstance)
                {
                    MessageBox.Show("程序已启动");
                    return;
                }
    
                Application.Run(new FrmLogin());
                GC.KeepAlive(mutex);
    
            }
        }
    
    
    }

    本文来自博客园,作者:Bin_x,转载请注明原文链接:https://www.cnblogs.com/Bin-x/p/3621092.html

  • 相关阅读:
    LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
    LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
    LeetCode 79 Word Search(单词查找)
    LeetCode 78 Subsets (所有子集)
    LeetCode 77 Combinations(排列组合)
    LeetCode 50 Pow(x, n) (实现幂运算)
    LeetCode 49 Group Anagrams(字符串分组)
    LeetCode 48 Rotate Image(2D图像旋转问题)
    LeetCode 47 Permutations II(全排列)
    LeetCode 46 Permutations(全排列问题)
  • 原文地址:https://www.cnblogs.com/Bin-x/p/3621092.html
Copyright © 2011-2022 走看看