[tomboy] Add GuiUtiles.GtkInvokeAndWait method, use to clean up SyncManager



commit 879811c325e3020a1da5ec32f0aaed84ad16d76e
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Mon Feb 8 11:30:31 2010 -0800

    Add GuiUtiles.GtkInvokeAndWait method, use to clean up SyncManager

 Tomboy/Synchronization/SyncManager.cs |   66 ++++----------------------------
 Tomboy/Utils.cs                       |   26 +++++++++++++
 2 files changed, 35 insertions(+), 57 deletions(-)
---
diff --git a/Tomboy/Synchronization/SyncManager.cs b/Tomboy/Synchronization/SyncManager.cs
index 901a995..758381c 100644
--- a/Tomboy/Synchronization/SyncManager.cs
+++ b/Tomboy/Synchronization/SyncManager.cs
@@ -383,10 +383,7 @@ 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 {
+				GuiUtils.GtkInvokeAndWait (() => {
 					// Make list of all local notes
 					List<Note> localNotes = new List<Note> (NoteMgr.Notes);
 
@@ -397,21 +394,12 @@ namespace Tomboy.Sync
 					foreach (Note note in localNotes) {
 						if (client.GetRevision (note) != -1 &&
 						!serverNotes.Contains (note.Id)) {
-
 							if (NoteSynchronized != null)
 								NoteSynchronized (note.Title, NoteSyncType.DeleteFromClient);
 							NoteMgr.Delete (note);
 						}
 					}
-					evt.Set ();
-				} catch (Exception e) {
-				mainThreadException = e;
-			}
-			                        });
-
-				evt.WaitOne ();
-				if (mainThreadException != null)
-					throw mainThreadException;
+				});
 
 				// TODO: Add following updates to syncDialog treeview
 
@@ -530,22 +518,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)
@@ -554,21 +530,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)
@@ -577,21 +541,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]