[tasque/transition: 93/213] Add Windows single app instance logic



commit 62680faeeedab807b08e966b391e67ae13740485
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sun Jul 29 18:24:41 2012 +0200

    Add Windows single app instance logic
    
    * split GtkApp in two classes which are conditionally built:
    	- GtkLinuxApplication: holds single app instance logic with dbus
    	- GtkWinApplication: holds single app instance logic with named events
    * GtkApplicationBase: Common base class
    * Program.cs: Add GtkWinApplication init

 .../{GtkApplication.cs => GtkApplicationBase.cs}   |   53 +-------------
 src/Tasque.Gtk/GtkLinuxApplication.cs              |   65 +++++++++++++++++
 src/Tasque.Gtk/GtkWinApplication.cs                |   75 ++++++++++++++++++++
 src/Tasque.Gtk/Tasque.Gtk.csproj                   |    7 ++-
 src/tasque/Program.cs                              |    4 +-
 5 files changed, 153 insertions(+), 51 deletions(-)
---
diff --git a/src/Tasque.Gtk/GtkApplication.cs b/src/Tasque.Gtk/GtkApplicationBase.cs
similarity index 65%
rename from src/Tasque.Gtk/GtkApplication.cs
rename to src/Tasque.Gtk/GtkApplicationBase.cs
index 8eaea57..158ed6c 100644
--- a/src/Tasque.Gtk/GtkApplication.cs
+++ b/src/Tasque.Gtk/GtkApplicationBase.cs
@@ -32,9 +32,9 @@ using Gtk;
 
 namespace Tasque
 {
-	public class GtkApplication : NativeApplication
-	{
-		public GtkApplication ()
+	public abstract class GtkApplicationBase : NativeApplication
+	{		
+		public GtkApplicationBase ()
 		{
 			confDir = Path.Combine (
 				Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "tasque");
@@ -70,59 +70,14 @@ namespace Tasque
 			}
 		}
 		
-		protected override bool IsRemoteInstanceRunning ()
-		{
-#if !WIN32
-			// Register Tasque RemoteControl
-			try {
-				remoteInstance = RemoteControl.Register ();
-				if (remoteInstance != null) {
-					remoteInstance.RemoteInstanceKnocked = HandleRemoteInstanceKnocked;
-					Debug.Write ("Tasque remote control active.");
-				} else {
-					// If Tasque is already running, open the tasks window
-					// so the user gets some sort of feedback when they
-					// attempt to run Tasque again.
-					RemoteControl remote = null;
-					try {
-						remote = RemoteControl.GetInstance ();
-						remote.KnockKnock ();
-					} catch {}
-
-					Debug.WriteLine ("Tasque is already running.  Exiting...");
-					return true;
-				}
-			} catch (Exception e) {
-				Debug.WriteLine ("Tasque remote control disabled (DBus exception): {0}", e.Message);
-			}
-			return false;
-#else
-			
-#endif
-		}
-
-		protected override void ShowMainWindow ()
-		{
-			TaskWindow.ShowWindow ();
-		}
-		
 		protected override event EventHandler RemoteInstanceKnocked;
 		
-		void HandleRemoteInstanceKnocked ()
+		protected void OnRemoteInstanceKnocked ()
 		{
 			if (RemoteInstanceKnocked != null)
 				RemoteInstanceKnocked (this, EventArgs.Empty);
 		}
 		
-		protected override void Dispose (bool disposing)
-		{
-			if (disposing)
-				remoteInstance.RemoteInstanceKnocked = null;
-		}
-
 		string confDir;
-#if !WIN32
-		RemoteControl remoteInstance;
-#endif
 	}
 }
diff --git a/src/Tasque.Gtk/GtkLinuxApplication.cs b/src/Tasque.Gtk/GtkLinuxApplication.cs
new file mode 100644
index 0000000..caec039
--- /dev/null
+++ b/src/Tasque.Gtk/GtkLinuxApplication.cs
@@ -0,0 +1,65 @@
+// 
+// GtkLinuxApplication.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using System.Diagnostics;
+
+namespace Tasque
+{
+	public class GtkLinuxApplication : GtkApplicationBase
+	{
+		protected override void Dispose (bool disposing)
+		{
+			if (disposing && remoteInstance != null)
+				remoteInstance = null;
+			
+			base.Dispose (disposing);
+		}
+		
+		protected override bool IsRemoteInstanceRunning ()
+		{
+			// Register Tasque RemoteControl
+			try {
+				remoteInstance = RemoteControl.Register ();
+				if (remoteInstance != null) {
+					remoteInstance.RemoteInstanceKnocked = OnRemoteInstanceKnocked;
+					Debug.WriteLine ("Tasque remote control created.");
+				} else {
+					RemoteControl remote = null;
+					try {
+						remote = RemoteControl.GetInstance ();
+						remote.KnockKnock ();
+					} catch {}
+					return true;
+				}
+			} catch (Exception e) {
+				Debug.WriteLine ("Tasque remote control disabled (DBus exception): {0}", e.Message);
+			}
+			return false;
+		}
+		
+		RemoteControl remoteInstance;
+	}
+}
diff --git a/src/Tasque.Gtk/GtkWinApplication.cs b/src/Tasque.Gtk/GtkWinApplication.cs
new file mode 100644
index 0000000..b2d78aa
--- /dev/null
+++ b/src/Tasque.Gtk/GtkWinApplication.cs
@@ -0,0 +1,75 @@
+// 
+// GtkWinApplication.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using System.Diagnostics;
+using System.Threading;
+
+namespace Tasque
+{
+	public class GtkWinApplication : GtkApplicationBase
+	{
+		protected override void Dispose (bool disposing)
+		{
+			if (disposing && waitHandle != null) {
+				waitHandle.Dispose ();
+				waitHandle = null;
+			}
+			
+			base.Dispose (disposing);
+		}
+		
+		protected override bool IsRemoteInstanceRunning ()
+		{
+			try {
+				waitHandle = EventWaitHandle.OpenExisting(waitHandleName);
+				waitHandle.Set();
+				return true;
+			} catch (WaitHandleCannotBeOpenedException) {
+				waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, waitHandleName);
+				Debug.WriteLine("EventWaitHandle created.");
+				
+				var porter = new Thread(new ThreadStart(WaitForAnotherInstance));
+				porter.Start();
+			}
+			return false;
+		}
+		
+		void WaitForAnotherInstance ()
+		{
+			while (!exiting) {
+				waitHandle.WaitOne ();
+				if (!exiting) {
+					Debug.WriteLine ("Another app instance has just knocked on the door.");
+					OnRemoteInstanceKnocked ();
+				}
+			}
+		}
+		
+		bool exiting;
+		EventWaitHandle waitHandle;
+		readonly string waitHandleName = "Tasque." + Environment.UserName;
+	}
+}
diff --git a/src/Tasque.Gtk/Tasque.Gtk.csproj b/src/Tasque.Gtk/Tasque.Gtk.csproj
index 1fe780d..39ed83a 100644
--- a/src/Tasque.Gtk/Tasque.Gtk.csproj
+++ b/src/Tasque.Gtk/Tasque.Gtk.csproj
@@ -99,6 +99,7 @@
     <None Include="..\..\data\sounds\notify.wav">
       <Link>data\sounds\notify.wav</Link>
     </None>
+    <None Include="Samples\Gtk3SingleInstanceSolution.cs" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Properties\AssemblyInfo.cs" />
@@ -113,7 +114,6 @@
     <Compile Include="CompletedTaskGroup.cs" />
     <Compile Include="CompletedTaskGroupModel.cs" />
     <Compile Include="DateButton.cs" />
-    <Compile Include="GtkApplication.cs" />
     <Compile Include="NoteDialog.cs" />
     <Compile Include="NoteWidget.cs" />
     <Compile Include="Preferences.cs" />
@@ -125,8 +125,13 @@
     <Compile Include="TaskTreeView.cs" />
     <Compile Include="TaskWindow.cs" />
     <Compile Include="Utilities.cs" />
+    <Compile Include="GtkApplicationBase.cs" />
+  </ItemGroup>
+  <ItemGroup Condition=" '$(Configuration)' == 'GtkWinDebug' or '$(Configuration)' == 'GtkWinRelease' ">
+    <Compile Include="GtkWinApplication.cs" />
   </ItemGroup>
   <ItemGroup Condition=" '$(Configuration)' == 'GtkLinuxDebug' or '$(Configuration)' == 'GtkLinuxRelease' ">
+    <Compile Include="GtkLinuxApplication.cs" />
     <Compile Include="RemoteControl.cs" />
   </ItemGroup>
   <ItemGroup>
diff --git a/src/tasque/Program.cs b/src/tasque/Program.cs
index 0ba8262..fd8d2e7 100644
--- a/src/tasque/Program.cs
+++ b/src/tasque/Program.cs
@@ -39,8 +39,10 @@ namespace Tasque
 			NativeApplication app;
 #if OSX
 			app = new OSXApplication ();
+#elif WIN32
+			app = new GtkWinApplication ();
 #else
-			app = new GtkApplication ();
+			app = new GtkLinuxApplication ();
 #endif
 			return app;
 		}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]