[Gtk-sharp-list] Gtk# threading problem?

Pablo Baena pbaena@uol.com.ar
10 Oct 2002 03:09:26 +0000


--=-FlQBQhGwm+CZKX++ZZT1
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Should this work? Because it doesn't.

Thank you!!!
Pablo


namespace test
{
	using Gtk;
	using Gdk;
	using GtkSharp;
	using System;
	using System.IO;
	using System.Drawing;
	using System.Runtime.InteropServices;
	using System.Diagnostics;
	using System.Threading;
	using Gnome;
=09
	class mainone
	{
		public static void Main(string[] args)
		{
			test_threads tt =3D new test_threads(args);
		}
	}

	class test_threads
	{
		public Gnome.App window =3D null;
		TextView textview =3D null;
		ThreadNotify tn =3D null;=20
		string output=3Dnull;
					=09
		public test_threads(string[] args)
		{
			tn=3Dnew ThreadNotify(new ReadyEvent(show_status));

			Program kit =3D new Program ("test", "0.0.1", Modules.UI,
				args);
			window =3D new Gnome.App ("test", "Test");
			window.SetDefaultSize (250, 200);=09
			window.DeleteEvent +=3D new DeleteEventHandler (Window_Delete);
		=09
			VBox box1 =3D new VBox (false, 0);

			Button btn =3D new Button ("Click Me");
			btn.Clicked +=3D new EventHandler (btn_click);
			box1.PackStart (btn, true, true, 4);

			ScrolledWindow sw =3D new ScrolledWindow ();
			textview =3D new TextView();
			textview.Editable =3D false;
			textview.WrapMode =3D Gtk.WrapMode.Char;
			textview.CursorVisible =3D false;
			sw.Add(textview);
			box1.PackStart (sw, true, true, 4);

			window.Contents=3Dbox1;
			window.ShowAll ();
			kit.Run();
		}=09

		void btn_click (object obj, EventArgs args)
		{
			Thread thr =3D new Thread (new ThreadStart (ThreadFunc));
			thr.Start ();
			Console.WriteLine ("Button Clicked");
		}

		void Window_Delete (object obj, DeleteEventArgs args)
		{
			Application.Quit ();
			args.RetVal =3D true;
		}

		public void show_status()
		{
			TextBuffer buff =3D textview.Buffer;
			Console.WriteLine("showing: "+output);
			buff.InsertAtCursor(output+'\n', -1);
		}
			=09
		public void ThreadFunc()
		{
			ProcessStartInfo psi =3D new ProcessStartInfo();
			psi.FileName=3D"ls";
			psi.Arguments=3D"/usr/share/doc";
			psi.WorkingDirectory=3D"/tmp/";
			psi.RedirectStandardOutput=3Dtrue;
			Process p =3D Process.Start(psi);
			string tmp;
			while ((tmp =3D p.StandardOutput.ReadLine())!=3Dnull) {
				Console.WriteLine("sending: "+tmp);
				output=3Dtmp;
				tn.WakeupMain();
				//show_status();
			}
		}
	}
}



On Wed, 2002-10-09 at 23:25, Miguel de Icaza wrote:

    > What I found out is that getting all the gnome stuff from my program
    > out, it does what it is meant. I mean, commenting all the gnome stuff
    > from my program.=20
   =20
    You are using Gtk from two threads (one is the main loop, and you are
    pushing data onto a widget in the other).  Gtk is not thread safe, you
    can not do this.
   =20
    You need to have your thread load the data, and when it is ready to
    process it, wake up the main thread and have the main thread (which has
    the Gtk main loop) feed the data to the widget.
   =20
    Look at the ThreadNotify class in Gtk# which simplifies the complexicty
    of doing this.
   =20
    Miguel
   =20
    _______________________________________________
    Gtk-sharp-list maillist  -  Gtk-sharp-list@ximian.com

http://lists.ximian.com/mailman/listinfo/gtk-sharp-list


________________________________________________________________________


<Tetsuo> la vida es muy ironica vio?
<Ranma> sip la verdad que se nos esta cagando de risa!
<Ranma> y la voy a cagar a pi=F1as

--=-FlQBQhGwm+CZKX++ZZT1
Content-Type: text/html; charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/1.0.4">
</HEAD>
<BODY>
Should this work? Because it doesn't.
<BR>

<BR>
Thank you!!!
<BR>
Pablo
<BR>

<PRE>namespace test
{
	using Gtk;
	using Gdk;
	using GtkSharp;
	using System;
	using System.IO;
	using System.Drawing;
	using System.Runtime.InteropServices;
	using System.Diagnostics;
	using System.Threading;
	using Gnome;
	
	class mainone
	{
		public static void Main(string[] args)
		{
			test_threads tt = new test_threads(args);
		}
	}

	class test_threads
	{
		public Gnome.App window = null;
		TextView textview = null;
		ThreadNotify tn = null; 
		string output=null;
						
		public test_threads(string[] args)
		{
			tn=new ThreadNotify(new ReadyEvent(show_status));

			Program kit = new Program (&quot;test&quot;, &quot;0.0.1&quot;, Modules.UI,
				args);
			window = new Gnome.App (&quot;test&quot;, &quot;Test&quot;);
			window.SetDefaultSize (250, 200);	
			window.DeleteEvent += new DeleteEventHandler (Window_Delete);
			
			VBox box1 = new VBox (false, 0);

			Button btn = new Button (&quot;Click Me&quot;);
			btn.Clicked += new EventHandler (btn_click);
			box1.PackStart (btn, true, true, 4);

			ScrolledWindow sw = new ScrolledWindow ();
			textview = new TextView();
			textview.Editable = false;
			textview.WrapMode = Gtk.WrapMode.Char;
			textview.CursorVisible = false;
			sw.Add(textview);
			box1.PackStart (sw, true, true, 4);

			window.Contents=box1;
			window.ShowAll ();
			kit.Run();
		}	

		void btn_click (object obj, EventArgs args)
		{
			Thread thr = new Thread (new ThreadStart (ThreadFunc));
			thr.Start ();
			Console.WriteLine (&quot;Button Clicked&quot;);
		}

		void Window_Delete (object obj, DeleteEventArgs args)
		{
			Application.Quit ();
			args.RetVal = true;
		}

		public void show_status()
		{
			TextBuffer buff = textview.Buffer;
			Console.WriteLine(&quot;showing: &quot;+output);
			buff.InsertAtCursor(output+'\n', -1);
		}
				
		public void ThreadFunc()
		{
			ProcessStartInfo psi = new ProcessStartInfo();
			psi.FileName=&quot;ls&quot;;
			psi.Arguments=&quot;/usr/share/doc&quot;;
			psi.WorkingDirectory=&quot;/tmp/&quot;;
			psi.RedirectStandardOutput=true;
			Process p = Process.Start(psi);
			string tmp;
			while ((tmp = p.StandardOutput.ReadLine())!=null) {
				Console.WriteLine(&quot;sending: &quot;+tmp);
				output=tmp;
				tn.WakeupMain();
				//show_status();
			}
		}
	}
}</PRE>

<BR>

<BR>
On Wed, 2002-10-09 at 23:25, Miguel de Icaza wrote:
    <BLOCKQUOTE>
<PRE><FONT COLOR="#737373"><FONT SIZE="3"><I>&gt; What I found out is that getting all the gnome stuff from my program</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>&gt; out, it does what it is meant. I mean, commenting all the gnome stuff</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>&gt; from my program. </FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I></FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>You are using Gtk from two threads (one is the main loop, and you are</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>pushing data onto a widget in the other).  Gtk is not thread safe, you</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>can not do this.</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I></FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>You need to have your thread load the data, and when it is ready to</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>process it, wake up the main thread and have the main thread (which has</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>the Gtk main loop) feed the data to the widget.</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I></FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>Look at the ThreadNotify class in Gtk# which simplifies the complexicty</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>of doing this.</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I></FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>Miguel</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I></FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>_______________________________________________</FONT></FONT></I>
<FONT COLOR="#737373"><FONT SIZE="3"><I>Gtk-sharp-list maillist  -  Gtk-sharp-list@ximian.com</FONT></FONT></I></PRE>
    </BLOCKQUOTE>
<A HREF="http://lists.ximian.com/mailman/listinfo/gtk-sharp-list"><FONT SIZE="3"><I>http://lists.ximian.com/mailman/listinfo/gtk-sharp-list</FONT></I></A>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>

<BR>

<HR>

<BR>

<PRE>&lt;Tetsuo&gt; la vida es muy ironica vio?
&lt;Ranma&gt; sip la verdad que se nos esta cagando de risa!
&lt;Ranma&gt; y la voy a cagar a pi&#241;as</PRE>
</TD>
</TR>
</TABLE>

</BODY>
</HTML>

--=-FlQBQhGwm+CZKX++ZZT1--