{"id":176,"date":"2010-05-24T19:18:07","date_gmt":"2010-05-24T10:18:07","guid":{"rendered":"http:\/\/astel-labs.net\/blog\/diary\/2010\/05\/100524-1.html"},"modified":"2012-03-11T20:40:54","modified_gmt":"2012-03-11T11:40:54","slug":"100524-1","status":"publish","type":"post","link":"http:\/\/astel-labs.net\/blog\/diary\/2010\/05\/100524-1.html","title":{"rendered":"WPF\u3067\u591a\u91cd\u8d77\u52d5\u9632\u6b62"},"content":{"rendered":"<p>\u81ea\u5206\u7528\u30e1\u30e2<\/p>\n<p>&nbsp;<\/p>\n<p>WindowsForms \u30a2\u30d7\u30ea\u3067\u5b9f\u88c5\u3057\u3066\u3044\u305f\u3001\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306b\u3088\u308b\u591a\u91cd\u8d77\u52d5\u9632\u6b62\u306e\u30b3\u30fc\u30c9\u3092 WPF \u306b\u66f8\u304d\u63db\u3048\u3066\u307f\u305f<\/p>\n<p>&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>WindowsForms \u3067\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u8a18\u8ff0\u3057\u3066\u3044\u305f\u3082\u306e\u3092\u3001<\/p>\n<pre class=\"brush: csharp\">\r\n[Guid( &quot;04669F2E-5017-4723-B3A1-DFF6200D8DA4&quot; )]\r\nstatic class Program\r\n{\r\n    private static Mutex m_SingleLock = null;\r\n\r\n    [STAThread]\r\n    static void Main()\r\n    {\r\n        Application.EnableVisualStyles();\r\n        Application.SetCompatibleTextRenderingDefault( false );\r\n\r\n        \/\/ \u591a\u91cd\u8d77\u52d5\u306e\u9632\u6b62\r\n        Attribute attribute = Attribute.GetCustomAttribute( typeof( Program ), typeof( GuidAttribute ) );\r\n        if( (m_SingleLock =\r\n            SingleInstanceLock( (new Guid( ((GuidAttribute)attribute).Value )).ToString() )) == null ) {\r\n            MessageBox.Show( &quot;\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u65e2\u306b\u5b9f\u884c\u4e2d\u3067\u3059\u3002&quot;, &quot;\u8d77\u52d5\u30a8\u30e9\u30fc&quot;,\r\n                MessageBoxButtons.OK, MessageBoxIcon.Information );\r\n            return;\r\n        }\r\n\r\n        \/\/ \u30d5\u30a9\u30fc\u30e0\u306e\u4f5c\u6210\r\n        Application.Run( new FormMain() );\r\n    }\r\n\r\n    private static Mutex SingleInstanceLock( string LockName )\r\n    {\r\n        bool CreatedNew;\r\n\r\n        try {\r\n            \/\/ \u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306e\u4f5c\u6210\r\n            Mutex SingleLock = new Mutex( true, LockName, out CreatedNew );\r\n            if( !CreatedNew )\r\n                return null;\r\n\r\n            return SingleLock;\r\n        }\r\n        catch {\r\n            return null;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>WPF \u3067\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u66f8\u304d\u63db\u3048\u307e\u3059<\/p>\n<pre class=\"brush: csharp\">\r\n[Guid( &quot;04669F2E-5017-4723-B3A1-DFF6200D8DA4&quot; )]\r\npublic partial class App : Application\r\n{\r\n    private readonly Mutex m_SingleLock = null;\r\n\r\n    public App()\r\n    {\r\n        \/\/ \u591a\u91cd\u8d77\u52d5\u306e\u9632\u6b62\r\n        Attribute attribute = Attribute.GetCustomAttribute( typeof( App ), typeof( GuidAttribute ) );\r\n        if( (m_SingleLock = SingleInstanceLock( (new Guid( ((GuidAttribute)attribute).Value )).ToString() )) == null ) {\r\n            MessageBox.Show( &quot;\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u65e2\u306b\u5b9f\u884c\u4e2d\u3067\u3059\u3002&quot;, &quot;\u8d77\u52d5\u30a8\u30e9\u30fc&quot;,\r\n                MessageBoxButton.OK, MessageBoxImage.Information );\r\n            this.Shutdown();\r\n        }\r\n    }\r\n\r\n    private Mutex SingleInstanceLock( string LockName )\r\n    {\r\n        bool CreatedNew;\r\n\r\n        try {\r\n            \/\/ \u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306e\u4f5c\u6210\r\n            Mutex SingleLock = new Mutex( true, LockName, out CreatedNew );\r\n            if( !CreatedNew )\r\n                return null;\r\n\r\n            return SingleLock;\r\n        }\r\n        catch {\r\n            return null;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>VisualStudio \u3067 WPF \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u305f\u969b\u306b\u4f5c\u6210\u3055\u308c\u308b\u3001App.xaml.cs \u3092\u958b\u304d\u3001App \u30af\u30e9\u30b9\u306b\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059<\/p>\n<p>\u3042\u3068\u306f WindowsForms \u30a2\u30d7\u30ea\u3068\u540c\u3058\u304f App \u30af\u30e9\u30b9\u5185\u306b\u3001\u591a\u91cd\u8d77\u52d5\u9632\u6b62\u7528\u306e\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u304c\u65e2\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u304b\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u95a2\u6570\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3001\u307b\u307c WindowsForms \u30a2\u30d7\u30ea\u3068\u540c\u3058\u3067\u3059<\/p>\n<p>&nbsp;<\/p>\n<p>\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306b\u95a2\u9023\u4ed8\u3051\u3066\u3044\u308b\u6587\u5b57\u5217\u306b\u3064\u3044\u3066\u306f\u3001\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u30af\u30e9\u30b9\u306b Guid \u3092\u95a2\u9023\u4ed8\u3051\u3066\u305d\u308c\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u4e00\u822c\u7684\u306b\u306f\u4ed6\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u885d\u7a81\u3057\u306a\u3044\u3088\u3046\u306a\u30e6\u30cb\u30fc\u30af\u306a\u540d\u79f0\u3092\u76f4\u63a5 LockName \u306b\u6307\u5b9a\u3057\u307e\u3059<\/p>\n<p>\u308f\u305f\u3057\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3054\u3068\u306b\u6bce\u56de\u540d\u79f0\u3092\u8003\u3048\u3066\u4ed8\u3051\u308b\u306e\u304c\u9762\u5012\u306a\u306e\u3067\u3001Guid \u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u3068\u308b\u306e\u304c\u597d\u307f\u3067\u3059<\/p>\n<p>\u305f\u3060\u3057\u3001WPF \u3067\u306f Guid \u5c5e\u6027\u3092\u8a18\u8ff0\u3057\u3066\u3082\u30a4\u30f3\u30c6\u30ea\u30bb\u30f3\u30b9\u304c\u50cd\u304b\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3001\u300cusing System.Runtime.InteropServices;\u300d\u3092\u8a18\u8ff0\u3057\u3066\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308b\u3088\u3046\u3067\u3059<\/p>\n<p>&nbsp;<\/p>\n<p>\u203bWPF \u7248\u306e\u591a\u91cd\u8d77\u52d5\u9632\u6b62\u306e\u30b3\u30fc\u30c9\u306f\u3001\u304d\u3061\u3093\u3068\u691c\u8a3c\u3057\u3066\u3044\u307e\u305b\u3093\u306e\u3067\u5229\u7528\u3055\u308c\u308b\u5834\u5408\u306f\u72ec\u81ea\u306b\u691c\u8a3c\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5206\u7528\u30e1\u30e2 &nbsp; WindowsForms \u30a2\u30d7\u30ea\u3067\u5b9f\u88c5\u3057\u3066\u3044\u305f\u3001\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306b\u3088\u308b\u591a\u91cd\u8d77\u52d5\u9632\u6b62\u306e\u30b3\u30fc\u30c9\u3092 WPF \u306b\u66f8\u304d\u63db\u3048\u3066\u307f\u305f &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4],"tags":[],"class_list":["post-176","post","type-post","status-publish","format-standard","hentry","category-wpf","category-4"],"_links":{"self":[{"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":1,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":214,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/176\/revisions\/214"}],"wp:attachment":[{"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}