[Mono-list] How to address the Gtk# Thread issue...

Moritz Angermann moritz.angermann@gmx.net
Mon, 15 Mar 2004 20:16:06 +0100


Hi List,
Well after asking many stupid question on #mono and #md-devel, and
giving google a few shots.
I found out a few solutions, with the help of you kind people,

Lets assume we Have a widget with a Label and a Button on it as glade
file.

So in out Main we load it up.

The button signal is connected so that it will
call an argumentedThread class and start it.
The class will just use the HttpWebRequest method to obtain
some data from the web. ( In wich case it takes some time,
and would freeze the gui if run from the gui thread. )

Now I found 3 ways to do it:

First: use GLib.Threads.Enter( ) and GLib.Threads.Leave( )
	Call the thread like:
	new Thread( new ThreadStart( new argThread( ref label ).Run ) ).Start(
);

	and in the Thread have: 
	GLib.Threads.Enter( );
	label.Text = WebResult.ToString( );
	GLib.Theads.Leave( );

Secound: try to use ThreadNotify( ) and Queue processing
	in the gui class have a 

	Queue queue = new Queue;
	ThreadNotify tn = new ThreadNotify( new ReadyEvent( processQueue ) );
	
	void processQueue ( )
	{
		lock( queue )
		{
			while( queue.Count > 0 )
			{
				queueItem tmp = (queueItem) queue.Dequeue( );
				// do something with the item ... 
				// Interpret, etc...
			}
		}
	}

	and call the thread like:
	new Thread( new ThreadStart( new argThread( ref tn, ref queue ).Run )
).Start( );
	
	the Thread itself than does:
	queueItem qi = new queueItem( args );
	lock(queue){ queue.Enqueue( qi ); }
	tn.WakeupMain( );

Third: using the GLib.Idle.Add

	we have a helper class.
	class helper( )
	{
		Label lbl = null;
		public helper( ref l )
		{
			this.lbl = l;
		}
		public bool update( )
		{
			lbl.Text = "...";
			return true;
		}
	}
	starting the Thread like:
	new Thread( new StartThread( new argThread( ref label ) ).Start( );
	and the thread than:
	GLib.Idle.Add( new IdleHandler( new heper( ref label ).update ) );


My question now is:
which is the prefered method?
Or am I doing something wrong ( wich is what I guess ).

To me the queue method looks like the cleanest.
the GLib.threads method seems to be the approach suggested by the GTK
FAQ. But you have to reference everything you want to update...

The third Method seems to turn pretty fast in unmaintainable code. At
least that how it looks to me...

kindest regards,
 -mo

-- 
----------------  contact info  ----------------
Moritz Angermann       	moritz.angermann@gmx.net
Liquid:Mint            	www.liquidmint.org
Mobile                 	+49 (0) 160 9197 5880
Home                   	+49 (0) 4322 75 12 66