[Kde-bindings] KDE/kdebindings/csharp/qyoto

Arno Rehn kde at arnorehn.de
Sat Oct 25 16:05:06 UTC 2008


SVN commit 875789 by arnorehn:

* Add QCoreApplication.Invoke() which executes a delegate in the same thread
  in which Q(Core)Application's main loop runs (usually the main thread).

CCMAIL: kde-bindings at kde.org
CCMAIL: eric at extremeboredom.net



 M  +2 -0      ChangeLog  
 M  +34 -0     core/QCoreApplicationExtras.cs  
 M  +3 -0      gui/QApplicationExtras.cs  


--- trunk/KDE/kdebindings/csharp/qyoto/ChangeLog #875788:875789
@@ -2,6 +2,8 @@
 
 	* Use List<IQGraphicsItem> instead of List<QGraphicsItem> and check for
 	  multiple inheritance in ConstructList(). Fixes bug reported by Eric Butler.
+	* Add QCoreApplication.Invoke() which executes a delegate in the same thread
+	  in which Q(Core)Application's main loop runs (usually the main thread).
 
 2008-08-29  Arno Rehn  <arno at arnorehn.de>
 
--- trunk/KDE/kdebindings/csharp/qyoto/core/QCoreApplicationExtras.cs #875788:875789
@@ -2,9 +2,42 @@
 
 	using System;
 	using System.Collections;
+	using System.Runtime.InteropServices;
 	using System.Text;
 
+	class EventReceiver : QObject {
+		public EventReceiver(QObject parent) : base(parent) {}
+		
+		public override bool Event(QEvent e) {
+			if (e.type() == QEvent.TypeOf.User) {
+				ThreadEvent my = (ThreadEvent) e;
+				my.dele();
+				my.handle.Free();  // free the handle so the event can be collected
+				return true;
+			}
+			return false;
+		}
+	}
+
+	class ThreadEvent : QEvent {
+		public NoArgDelegate dele;
+		public GCHandle handle;
+		
+		public ThreadEvent(NoArgDelegate d) : base(QEvent.TypeOf.User) {
+			dele = d;
+		}
+	}
+
 	public partial class QCoreApplication : QObject, IDisposable {
+		private EventReceiver receiver;
+		protected void SetupEventReceiver() { receiver = new EventReceiver(this); }
+
+		public static void Invoke(NoArgDelegate dele) {
+			ThreadEvent e = new ThreadEvent(dele);
+			e.handle = GCHandle.Alloc(e);  // we don't want the GC to collect the event too early
+			PostEvent(qApp.receiver, e);
+		}
+
 		public QCoreApplication(string[] argv) : this((Type) null) {
 			CreateProxy();
 			
@@ -15,6 +48,7 @@
 			interceptor.Invoke(	"QCoreApplication$?", 
 								"QCoreApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args );
+			SetupEventReceiver();
 		}
 	}
 }
--- trunk/KDE/kdebindings/csharp/qyoto/gui/QApplicationExtras.cs #875788:875789
@@ -17,6 +17,7 @@
 			interceptor.Invoke(	"QApplication$?", 
 								"QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args );
+			SetupEventReceiver();
 		}
 
 		public QApplication(string[] argv, bool GUIenabled) : this((Type) null) {
@@ -30,6 +31,7 @@
 			interceptor.Invoke(	"QApplication$?", 
 								"QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(bool), GUIenabled );
+			SetupEventReceiver();
 		}
     
 		public QApplication(string[] argv, QApplication.TypeOf arg3) : this((Type) null) {
@@ -43,6 +45,7 @@
 			interceptor.Invoke(	"QApplication$?", 
 								"QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(QApplication.TypeOf), arg3 );
+			SetupEventReceiver();
 		}   
 	}
 }



More information about the Kde-bindings mailing list