{"id":219,"date":"2013-04-17T07:34:25","date_gmt":"2013-04-16T23:34:25","guid":{"rendered":"http:\/\/bohu.net\/blog\/?p=219"},"modified":"2013-04-17T07:34:25","modified_gmt":"2013-04-16T23:34:25","slug":"%e8%b0%83%e7%94%a8-vb-net-%e4%b8%ad%e7%9a%84-issingleinstance-%e5%ae%9e%e7%8e%b0%e5%8f%aa%e8%bf%90%e8%a1%8c%e5%8d%95%e4%b8%aa%e5%ae%9e%e4%be%8b%e7%9a%84%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.bohu.net\/blog\/219\/","title":{"rendered":"\u5728C#\u4e2d\u8c03\u7528 VB.Net \u4e2d\u7684 IsSingleInstance \u5b9e\u73b0\u53ea\u8fd0\u884c\u5355\u4e2a\u5b9e\u4f8b\u7684\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p>\u4ece <a href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=474650&amp;seqNum=2\" target=\"_blank\">&lt;&lt;Windows Forms 2.0 Programming, 2nd Edition&gt;&gt;\u00a0\u00a0\u00a0&#8211;\u00a0 Single-Instance Applications<\/a> \u8fd9\u4e00\u7ae0\u4e2d, \u5b66\u5230\u4e86\u8c03\u7528 VB.Net \u4e2d\u7684 IsSingleInstance, \u4e3a C# WinForm\u00a0\u6dfb\u52a0\u53ea\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u5355\u4e2a\u5b9e\u4f8b ( Single Instance Application). \u662f\u4e2a\u597d\u65b9\u6cd5!<\/p>\n<p><strong><span style=\"color: #800000;\">\u8be5\u65b9\u6cd5\u663e\u7136\u4ece\u6613\u7528\u6027\u4e0a\u4fbf\u6377\u4e0e Mutex \u548c Process \u8fd9\u4e24\u79cd\u53ea\u8fd0\u884c\u5355\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\u7684\u65b9\u6cd5.<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Single Instance<\/strong> \u6982\u5ff5:<\/p>\n<p>\u4ece.NET 2.0\u8d77\uff0c\u63d0\u4f9b\u4e86WindowsFormsApplicationBase\u7c7b\u6765\u7b80\u5316Windows\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\uff0c\u5982\u679c\u60a8\u662f\u5f00\u53d1\u4eba\u5458\u4f1a\u611f\u5230\u611f\u5230\u5947\u602a\uff0cWindowsFormsApplicationBase\u7c7b\u4e0d\u5728System.Windows.Forms \u547d\u540d\u7a7a\u95f4\u4e2d\u800c\u662f\u5c5e\u4e8eMicrosoft.VisualBasic.ApplicationServices \u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u8bb8\u8fd9\u662f\u4f5c\u4e3aVB.NET\u5f00\u53d1\u4eba\u5458\u7684\u4f18\u5148\u597d\u5904\u5427\u3002\u8be5\u7c7b\u5bf9\u5e94\u7684\u7a0b\u5e8f\u96c6\u4e3aMicrosoft.VisualBasic.dll\uff0c\u4e0d\u8fc7\u8be5\u7a0b\u5e8f\u96c6\u5305\u542b\u5728.NET\u6846\u67b6\u4e2d\u4e00\u8d77\u53d1\u5e03\uff0c\u5982\u679c\u8981\u5f15\u7528\u8be5\u7a0b\u5e8f\u96c6\uff0c\u5728\u90e8\u7f72\u4e0a\u4e0d\u5b58\u5728\u989d\u5916\u64cd\u4f5c\u3002<\/p>\n<p>WindowsFormsApplicationBase\u7c7b\u5b9e\u73b0\u4e86\u7c7b\u4f3c\u4e8eApplication\u7c7b\u7684\u4e00\u4e9b\u529f\u80fd\uff0c\u4e0d\u8fc7\u8be5\u7c7b\u8fd8\u5305\u542b\u4e00\u4e9b\u7b80\u5316Windows Forms\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u63a5\u53e3\uff0c\u4e0b\u9762\u6765\u7b80\u5355\u4e86\u89e3\u4e00\u4e0b\u3002WindowsFormsApplicationBase\u7c7b\u5b9e\u73b0\u4e86\u5bf9\u5355\u5b9e\u4f8b\u5e94\u7528\u7a0b\u5e8f\u7684\u652f\u6301\uff0c\u901a\u8fc7\u8bbe\u7f6eIsSingleInstance \u5c5e\u6027\u4e3aTrue\u4ee5\u53ca\u91cd\u5199OnStartupNextInstance\u65b9\u6cd5\u53ef\u4ee5\u7b80\u6d01\u7684\u5b9e\u73b0\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>\u5b9e\u73b0<\/strong><\/h2>\n<p>\u5728 Program.cs\u00a0&#8211; <b>Main \u65b9\u6cd5<\/b>\u4e2d<\/p>\n<p>1. \u9879\u76ee\u4e2d\u5f15\u7528VB.Net \u7684DLL &#8211; Microsoft.VisualBasic.DLL,<\/p>\n<p><em>Program.cs<\/em><\/p>\n<pre>using Microsoft.VisualBasic.ApplicationServices;<\/pre>\n<p>2. \u5728Program.cs\u4e2d\u589e\u52a0\u4e00\u4e2a\u7c7b<\/p>\n<p><em>Program.cs<\/em><\/p>\n<pre class=\"brush:csharp\">public sealed class SingleInstanceApplication : WindowsFormsApplicationBase\n{\n    public SingleInstanceApplication()\n    {\n        base.IsSingleInstance = true;\n        base.ShutdownStyle = ShutdownMode.AfterMainFormCloses;\n    }\n\n    protected override void OnCreateMainForm()\n    {\n        base.MainForm = new MainForm();\n    }\n\n    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)\n    {\n        base.OnStartupNextInstance(e);\n        base.MainForm.Activate();\n    }\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>3. \u4fee\u6539\u539f Application.Run(new MainForm()); \u65b9\u6cd5\u4e3a:<\/p>\n<pre class=\"brush:csharp\">\/\/\u6dfb\u52a0\u8fd0\u884c\u5355\u8fdb\u7a0b\u7a0b\u5e8f\nSingleInstanceApplication application = new SingleInstanceApplication();\napplication.Run(args);<\/pre>\n<p>&nbsp;<\/p>\n<p>SingleInstanceApplication\u7c7b\u7ee7\u627f\u81eaWindowsFormsApplicationBase\uff0c\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8bbe\u7f6e\u4e3a\u5355\u5b9e\u4f8b\u6a21\u5f0f\uff0c\u540c\u65f6\u8bbe\u7f6e\u5728\u4e3b\u7a97\u4f53\u5173\u95ed\u540e\u9000\u51fa\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u7ee7\u627f\u7c7b\u4e2d\uff0cOnCreateMainForm\u65b9\u6cd5\u88ab\u91cd\u5199\u7528\u6765\u521b\u5efa\u4e3b\u7a97\u4f53\uff0c\u5982\u679c\u8981\u4fdd\u8bc1\u5e94\u7528\u7a0b\u5e8f\u5355\u4e00\u5b9e\u4f8b\u8fd0\u884c\uff0c\u8fd8\u9700\u8981\u91cd\u5199OnStartupNextInstance\u65b9\u6cd5\uff0c\u5728\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0b\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\u542f\u52a8\u65f6\uff0cOnStartupNextInstance\u65b9\u6cd5\u4f1a\u5f97\u5230\u6267\u884c\uff0c\u5728\u4e0a\u9762\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528\u57fa\u7c7b\u65b9\u6cd5\u540c\u65f6\u6fc0\u6d3b\u4e3b\u7a97\u53e3\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece &lt;&lt;Windows Forms 2.0 Programming, 2nd Edition&#038;gt &hellip; <a href=\"https:\/\/www.bohu.net\/blog\/219\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5728C#\u4e2d\u8c03\u7528 VB.Net \u4e2d\u7684 IsSingleInstance \u5b9e\u73b0\u53ea\u8fd0\u884c\u5355\u4e2a\u5b9e\u4f8b\u7684\u5e94\u7528\u7a0b\u5e8f\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23],"tags":[50,98,126,208,397,417,419,425,459,532,616],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-net","category-23","tag-net","tag-c","tag-dll","tag-issingleinstance","tag-vb","tag-windows","tag-windows-forms","tag-winform","tag-459","tag-532","tag-616"],"_links":{"self":[{"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":0,"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bohu.net\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}