{"id":775,"date":"2012-12-07T21:34:19","date_gmt":"2012-12-07T13:34:19","guid":{"rendered":"http:\/\/www.vcoo.cc\/blog\/?p=775"},"modified":"2012-12-07T21:34:19","modified_gmt":"2012-12-07T13:34:19","slug":"csharp-only-run-one-app","status":"publish","type":"post","link":"https:\/\/vcoo.cc\/blog\/775\/","title":{"rendered":"C# \u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b\u7684\u65b9\u6cd5\u6536\u96c6"},"content":{"rendered":"<p>\u8fd9\u91cc\u5fae\u9177\u5206\u4eab\u4e00\u4e0b.Net\u6280\u672f\uff08C#\uff09\u5f00\u53d1WinForm\u5e94\u7528\u7a0b\u5e8f\u53ea\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002@VCOO<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528\u7ebf\u7a0b\u4e92\u65a5\u53d8\u91cf\uff0c\u901a\u8fc7\u5b9a\u4e49\u4e92\u65a5\u53d8\u91cf\u6765\u5224\u65ad\u662f\u5426\u5df2\u8fd0\u884c\u5b9e\u4f8b\u3002<\/strong><br \/>\n[csharp]<br \/>\n\/\/\/ &lt;summary&gt;<br \/>\n\/\/\/ \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\u3002<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\n[STAThread]<br \/>\nstatic void Main()<br \/>\n{<br \/>\n    bool RunOne;<br \/>\n    System.Threading.Mutex run = new System.Threading.Mutex(true, &quot;9a4822d4-ba21-44bc-b84a-ae1d67eedf0a&quot;, out RunOne);<br \/>\n    if (RunOne)<br \/>\n    {<br \/>\n        Application.EnableVisualStyles();<br \/>\n        Application.SetCompatibleTextRenderingDefault(false);<br \/>\n        Application.Run(new Form1());<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        Application.EnableVisualStyles();<br \/>\n        MessageBox.Show(&quot;\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b\u3002&quot;, &quot;\u7cfb\u7edf\u63d0\u793a\uff1a&quot;);<br \/>\n    }<br \/>\n}[\/csharp]<\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u5728Main()\u65b9\u6cd5\u5f00\u59cb\u65f6\u904d\u5386\u6240\u6709\u8fdb\u7a0b\uff0c\u83b7\u53d6\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u7a0b\u5e8f\u96c6GUID\u548cPID\uff0c\u82e5\u53d1\u73b0\u6709\u8ddf\u81ea\u5df1GUID\u76f8\u540c\u4e14PID\u4e0d\u540c\u7684\u8fdb\u7a0b\uff0c\u5c31\u52d2\u4ee4\u81ea\u8eab\u9000\u51fa\u3002<\/strong><br \/>\n[csharp]<br \/>\n\/\/\/ &lt;summary&gt;<br \/>\n\/\/\/ \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\u3002<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\n[STAThread]<br \/>\nstatic void Main()<br \/>\n{<br \/>\n    Guid ownGUID = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute))).Value);<br \/>\n    Guid proGUID;<br \/>\n    int ownPID = Process.GetCurrentProcess().Id;<br \/>\n    int proPID;<\/p>\n<p>    foreach (Process p in Process.GetProcesses())<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            proGUID = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(Assembly.LoadFile(p.MainModule.FileName), typeof(GuidAttribute))).Value);<br \/>\n            proPID = p.Id;<br \/>\n            if (proGUID.Equals(ownGUID) &amp;&amp; proPID != ownPID)<br \/>\n            {<br \/>\n                MessageBox.Show(&quot;\u7a0b\u5e8f\u5df2\u8fd0\u884c&quot;);<br \/>\n                Environment.Exit(Environment.ExitCode);<br \/>\n            }<br \/>\n        }<br \/>\n        catch<br \/>\n        {<br \/>\n            continue;  \/\/\u9047\u4e0a\u8fdb\u7a0b\u8bbf\u95ee\u5f02\u5e38\u5c31\u8df3\u8fc7\u8be5\u8fdb\u7a0b<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/\u82e5\u672a\u88abExit\uff0c\u6b63\u5e38\u542f\u52a8<br \/>\n    Application.EnableVisualStyles();<br \/>\n    Application.SetCompatibleTextRenderingDefault(false);<br \/>\n    Application.Run(new Form1());<br \/>\n}[\/csharp]<\/p>\n<p><strong>\u65b9\u6cd5\u4e09\uff1a\u5168\u5c40\u539f\u5b50\u6cd5\uff0c\u521b\u5efa\u7a0b\u5e8f\u524d\uff0c\u5148\u68c0\u67e5\u5168\u5c40\u539f\u5b50\u8868\u4e2d\u770b\u662f\u5426\u5b58\u5728\u7279\u5b9a\u539f\u5b50A<\/strong>\uff08\u521b\u5efa\u65f6\u6dfb\u52a0\u7684\uff09\uff0c \u5b58\u5728\u65f6\u505c\u6b62\u521b\u5efa\uff0c\u8bf4\u660e\u8be5\u7a0b\u5e8f\u5df2\u8fd0\u884c\u4e86\u4e00\u4e2a\u5b9e\u4f8b\uff1b\u4e0d\u5b58\u5728\u5219\u8fd0\u884c\u7a0b\u5e8f\u5e76\u60f3\u5168\u5c40\u539f\u5b50\u8868\u4e2d\u6dfb\u52a0\u7279\u5b9a\u539f\u5b50A\uff1b \u9000\u51fa\u7a0b\u5e8f\u65f6\u8981\u8bb0\u5f97\u91ca\u653e\u7279\u5b9a\u7684\u539f\u5b50A\u54e6\uff0c\u4e0d\u7136\u8981\u5230\u5173\u673a\u624d\u4f1a\u91ca\u653e\u3002<br \/>\n[csharp]<br \/>\n[System.Runtime.InteropServices.DllImport(&quot;kernel32.dll&quot;)]<br \/>\npublic static extern UInt32 GlobalAddAtom(String lpString);  \/\/\u6dfb\u52a0\u539f\u5b50<\/p>\n<p>[System.Runtime.InteropServices.DllImport(&quot;kernel32.dll&quot;)]<br \/>\npublic static extern UInt32 GlobalFindAtom(String lpString);  \/\/\u67e5\u627e\u539f\u5b50<\/p>\n<p>[System.Runtime.InteropServices.DllImport(&quot;kernel32.dll&quot;)]<br \/>\npublic static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);  \/\/\u5220\u9664\u539f\u5b50<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\n\/\/\/ \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\u3002<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\n[STAThread]<br \/>\nstatic void Main()<br \/>\n{<br \/>\n    if (GlobalFindAtom(&quot;xinggang&quot;) == 20121212) \/\/\u6ca1\u627e\u5230\u539f\u5b50&quot;xinggang&quot;<br \/>\n    {<br \/>\n        GlobalAddAtom(&quot;xinggang&quot;);  \/\/\u6dfb\u52a0\u539f\u5b50&quot;xinggang&quot;<br \/>\n        Application.EnableVisualStyles();<br \/>\n        Application.SetCompatibleTextRenderingDefault(false);<br \/>\n        Application.Run(new Form1());<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        MessageBox.Show(&quot;\u5df2\u7ecf\u8fd0\u884c\u4e86\u4e00\u4e2a\u5b9e\u4f8b\u4e86\u3002&quot;);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/\u6ce8\u610f\u8981\u5728FormClosed\u4e8b\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>GlobalDeleteAtom(GlobalFindAtom(&quot;xinggang&quot;));  \/\/\u5220\u9664\u539f\u5b50&quot;xinggang&quot;<br \/>\n[\/csharp]<\/p>\n<p><strong>\u65b9\u6cd5\u56db\uff1a\u6309\u8fdb\u7a0b\u540d\u67e5\u627e\uff0c\u5982\u679c\u5df2\u7ecf\u8fd0\u884c\u5219\u6fc0\u6d3b\u3002<\/strong><\/p>\n<p>[csharp]<br \/>\nusing System;<br \/>\nusing System.Windows.Forms;<br \/>\nusing System.Runtime.InteropServices;\/\/\u4f7f\u7528DllImport\u7684\u5fc5\u987b\u3002<br \/>\nusing System.Diagnostics;\/\/\u5f15\u5165Process \u7c7b<\/p>\n<p>namespace WindowsFormApp1<br \/>\n{<br \/>\n    static class Program<br \/>\n    {<br \/>\n        private const int WS_SHOWNORMAL = 1;<br \/>\n        [DllImport(&quot;User32.dll&quot;)]<br \/>\n        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);<br \/>\n        [DllImport(&quot;User32.dll&quot;)]<br \/>\n        private static extern bool SetForegroundWindow(IntPtr hWnd);<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\u3002<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        [STAThread]<br \/>\n        static void Main()<br \/>\n        {<br \/>\n            Process instance = GetRunningInstance();<br \/>\n            if (instance == null)<br \/>\n            {<br \/>\n                Application.EnableVisualStyles();<br \/>\n                Application.SetCompatibleTextRenderingDefault(false);<br \/>\n                Application.Run(new frm_Main());\/\/\u5728\u8fd9\u542f\u52a8\u4e3b\u7a97\u4f53\u3002<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                HandleRunningInstance(instance);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u83b7\u53d6\u5f53\u524d\u662f\u5426\u5177\u6709\u76f8\u540c\u8fdb\u7a0b\u3002<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;<br \/>\n        public static Process GetRunningInstance()<br \/>\n        {<br \/>\n            Process current = Process.GetCurrentProcess();<br \/>\n            Process[] processes = Process.GetProcessesByName(current.ProcessName);<br \/>\n            \/\/\u904d\u5386\u6b63\u5728\u6709\u76f8\u540c\u540d\u5b57\u8fd0\u884c\u7684\u4f8b\u7a0b<br \/>\n            foreach (Process process in processes)<br \/>\n            {<br \/>\n                \/\/\u5ffd\u7565\u73b0\u6709\u7684\u4f8b\u7a0b<br \/>\n                if (process.Id != current.Id)<br \/>\n                    \/\/\u786e\u4fdd\u4f8b\u7a0b\u4eceEXE\u6587\u4ef6\u8fd0\u884c<br \/>\n                    if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(&quot;\/&quot; , &quot;\\\\&quot;) == current.MainModule.FileName )<br \/>\n                        return process;<br \/>\n            }<br \/>\n            return null;<br \/>\n        }<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u6fc0\u6d3b\u539f\u6709\u7684\u8fdb\u7a0b\u3002<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        \/\/\/ &lt;param name=&quot;instance&quot;&gt;&lt;\/param&gt;<br \/>\n        public static void HandleRunningInstance(Process instance)<br \/>\n        {<br \/>\n            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);<br \/>\n            SetForegroundWindow(instance.MainWindowHandle);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u5fae\u9177\u5206\u4eab\u4e00\u4e0b.Net\u6280\u672f\uff08C#\uff09\u5f00\u53d1WinForm\u5e94\u7528\u7a0b\u5e8f\u53ea\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002@VCOO<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[195,201,51],"class_list":["post-775","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-csharp-2","tag-dotnet","tag-51","cat-13-id"],"_links":{"self":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":0,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"wp:attachment":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}