[tomboy/autosync: 14/15] Add new GuiUtils.GtkInvokeAndWait method, clean up some sync code to use it



commit 9eaac8c1e67b6c3f21e93d272dea6324d5ea6a5c
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Mon Feb 8 00:48:15 2010 -0800

    Add new GuiUtils.GtkInvokeAndWait method, clean up some sync code to use it

 Tomboy/Synchronization/SyncManager.cs |   50 ++++----------------------------
 Tomboy/Utils.cs                       |   26 +++++++++++++++++
 2 files changed, 33 insertions(+), 43 deletions(-)
---
diff --git a/Tomboy/Synchronization/SyncManager.cs b/Tomboy/Synchronization/SyncManager.cs
index af758f4..b45e88d 100644
--- a/Tomboy/Synchronization/SyncManager.cs
+++ b/Tomboy/Synchronization/SyncManager.cs
@@ -586,22 +586,10 @@ namespace Tomboy.Sync
 			// delegate to run in the main gtk thread.
 			// To be consistent, any exceptions in the delgate will be caught
 			// and then rethrown in the synchronization thread.
-			Exception mainThreadException = null;
-			AutoResetEvent evt = new AutoResetEvent (false);
-			Gtk.Application.Invoke (delegate {
-				try {
-					Note existingNote = NoteMgr.CreateWithGuid (noteUpdate.Title, noteUpdate.UUID);
-					UpdateLocalNote (existingNote, noteUpdate, NoteSyncType.DownloadNew);
-				} catch (Exception e) {
-					mainThreadException = e;
-				}
-
-				evt.Set ();
+			GuiUtils.GtkInvokeAndWait (() => {
+				Note existingNote = NoteMgr.CreateWithGuid (noteUpdate.Title, noteUpdate.UUID);
+				UpdateLocalNote (existingNote, noteUpdate, NoteSyncType.DownloadNew);
 			});
-
-			evt.WaitOne ();
-			if (mainThreadException != null)
-				throw mainThreadException;
 		}
 
 		private static void UpdateNoteInMainThread (Note existingNote, NoteUpdate noteUpdate)
@@ -610,21 +598,9 @@ namespace Tomboy.Sync
 			// delegate to run in the main gtk thread.
 			// To be consistent, any exceptions in the delgate will be caught
 			// and then rethrown in the synchronization thread.
-			Exception mainThreadException = null;
-			AutoResetEvent evt = new AutoResetEvent (false);
-			Gtk.Application.Invoke (delegate {
-				try {
-					UpdateLocalNote (existingNote, noteUpdate, NoteSyncType.DownloadModified);
-				} catch (Exception e) {
-					mainThreadException = e;
-				}
-
-				evt.Set ();
+			GuiUtils.GtkInvokeAndWait (() => {
+				UpdateLocalNote (existingNote, noteUpdate, NoteSyncType.DownloadModified);
 			});
-
-			evt.WaitOne ();
-			if (mainThreadException != null)
-				throw mainThreadException;
 		}
 
 		private static void DeleteNoteInMainThread (Note existingNote)
@@ -633,21 +609,9 @@ namespace Tomboy.Sync
 			// delegate to run in the main gtk thread.
 			// To be consistent, any exceptions in the delgate will be caught
 			// and then rethrown in the synchronization thread.
-			Exception mainThreadException = null;
-			AutoResetEvent evt = new AutoResetEvent (false);
-			Gtk.Application.Invoke (delegate {
-				try {
-					NoteMgr.Delete (existingNote);
-				} catch (Exception e) {
-					mainThreadException = e;
-				}
-
-				evt.Set ();
+			GuiUtils.GtkInvokeAndWait (() => {
+				NoteMgr.Delete (existingNote);
 			});
-
-			evt.WaitOne ();
-			if (mainThreadException != null)
-				throw mainThreadException;
 		}
 
 		private static void UpdateLocalNote (Note localNote, NoteUpdate serverNote, NoteSyncType syncType)
diff --git a/Tomboy/Utils.cs b/Tomboy/Utils.cs
index 92754b9..6dbbf5e 100644
--- a/Tomboy/Utils.cs
+++ b/Tomboy/Utils.cs
@@ -4,6 +4,7 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Threading;
 using System.IO;
 using System.Xml;
 
@@ -236,6 +237,31 @@ namespace Tomboy
 
 			return pretty_str;
 		}
+
+		/// <summary>
+		/// Invoke a method on the GUI thread, and wait for it to
+		/// return. If the method raises an exception, it will be
+		/// thrown from this method.
+		/// </summary>
+		/// <param name="a">
+		/// The action to invoke.
+		/// </param>
+		public static void GtkInvokeAndWait (Action a)
+		{
+			Exception mainThreadException = null;
+			AutoResetEvent evt = new AutoResetEvent (false);
+			Gtk.Application.Invoke (delegate {
+				try {
+					a.Invoke ();
+				} catch (Exception e) {
+					mainThreadException = e;
+				}
+				evt.Set ();
+			});
+			evt.WaitOne ();
+			if (mainThreadException != null)
+				throw mainThreadException;
+		}
 	}
 
 	public class GlobalKeybinder



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