[Gtk-sharp-list] A first go at gtk-sharp

Joop PG4I Joop PG4I <joop.pg4i@gmail.com>
Sat, 26 Mar 2005 20:43:28 +0100

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I have created a first gtk-sharp program.Programming in C-sharp using
GTK is surprisingly easy once you get the hang of it. My program is a
tiny convers network client which uses networking and threading.  The
GTK GUI is updated using the recommended ThreadNotify function.

Some parts I don't quite understand: why do I need "Thread.Sleep (50)"
in ReceiveChat ()? If I leave it out, the TextView gets updated  with
only part of the nework information,especially when received network
information consists of more than about 10 lines. Adding a Sleep here
would mean that a slower system would need more sleep than a fast
system. How do I avoid this?

Linux for your hamradio desktop

Content-Type: text/x-csharp; name="mconvers.cs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="mconvers.cs"

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using Gtk;

class Mconvers {
=09public static void Main(string[] args)
=09=09if (args.Length < 2)
=09=09=09Console.WriteLine ("Usage: mconvers [hostname] [port]");
=09=09Application.Init ();
=09=09new Convers (args);
=09=09Application.Run ();

public class Convers : Window
=09private TcpClient client;
=09private Statusbar sb;
=09private bool connected =3D false;
=09private NetworkStream ns;
=09private StreamReader reader;
=09private StreamWriter writer;
=09private Thread receive;
=09private TextBuffer b;
=09private TextView tv;
=09private Entry e;
=09private ScrolledWindow sw;
=09private ThreadNotify notify;
=09private Button btn;
=09private string data;
=09private string hostname;
=09private int port;

=09public Convers (string[] s) : base ("mconvers")
=09=09this.SetDefaultSize (500, 300);
=09=09this.DeleteEvent +=3D new DeleteEventHandler (OnMyWindowDelete);
=09=09VBox vb =3D new VBox (false, 0);
=09=09this.Add (vb);
=09=09sw =3D new ScrolledWindow ();
=09=09sw.ShadowType =3D ShadowType.In;
=09=09sw.HscrollbarPolicy =3D PolicyType.Never;
=09=09sw.VscrollbarPolicy =3D PolicyType.Always;
=09=09vb.PackStart (sw, true, true, 0);
=09=09tv =3D new TextView ();
=09=09b =3D tv.Buffer;
=09=09tv.Editable =3D false;
=09=09tv.WrapMode =3D WrapMode.Char;
=09=09tv.CursorVisible =3D false;
=09=09tv.CanFocus =3D false;
=09=09sw.Add (tv);
=09=09e =3D new Entry ();
=09=09e.Activated +=3D new EventHandler (OnText);
=09=09vb.PackStart (e, false, true, 0);
=09=09e.GrabFocus ();
=09=09sb =3D new Statusbar ();
=09=09vb.PackStart (sb, false, true, 0);
=09=09notify =3D new ThreadNotify (new ReadyEvent (ready));
=09=09hostname =3D s[0];
=09=09port =3D Convert.ToInt16 (s[1]);
=09=09this.Shown +=3D new EventHandler (connect);
=09=09this.ShowAll ();

=09private void sbupdate (string s)
=09=09sb.Pop (1);
=09=09sb.Push (1, s);
=09static void OnMyWindowDelete (object o, DeleteEventArgs args)
=09=09Application.Quit ();

=09private void OnText (object o, EventArgs args)
=09=09if (connected)
=09=09=09writer.WriteLine (e.Text + "\n");
=09=09=09writer.Flush ();
=09=09e.DeleteText (0, -1);

=09private void ready ()
=09=09TextMark EndMark;
=09=09b.Insert (b.EndIter, data);
=09=09b.Insert (b.EndIter, "\r\n");
=09=09EndMark =3D b.CreateMark ("end", b.EndIter, false);
=09=09tv.ScrollToMark (EndMark, 0.4, true, 0.0, 1.0);
=09=09b.DeleteMark (EndMark);

=09private void ReceiveChat ()
=09=09=09=09data =3D "";
=09=09=09=09while (connected && (data =3D reader.ReadLine ()) !=3D null)
=09=09=09=09=09notify.WakeupMain ();
=09=09=09=09=09Thread.Sleep (50);
=09=09=09catch (IOException e)
=09=09=09=09sbupdate ("IOException: " + e.Message);
=09=09=09=09sbupdate ("Connection closed");
=09=09=09=09connected =3D false;
=09=09catch (ThreadAbortException)
=09=09=09Thread.ResetAbort ();
=09=09=09sbupdate ("ReadThread Aborted");

=09private void connect (object obj, EventArgs args)
=09=09=09IPHostEntry hostinfo =3D=20
=09=09=09=09Dns.GetHostByName (hostname);
=09=09=09client =3D new TcpClient();
=09=09=09client.NoDelay =3D true;
=09=09=09client.ReceiveTimeout =3D 600000;
=09=09=09client.SendTimeout =3D 600000;
 =09=09=09client.Connect (hostinfo.AddressList[0], port);
=09=09=09ns =3D client.GetStream();
=09=09=09reader =3D new StreamReader (ns);
=09=09=09writer =3D new StreamWriter (ns);
=09=09=09connected =3D true;
=09=09catch (Exception e)
=09=09=09sbupdate ("Connection refused");
=09=09if (connected)
=09=09=09=09sbupdate ("Connected");
=09=09=09=09receive =3D new Thread (new ThreadStart (ReceiveChat));
=09=09=09=09receive.IsBackground =3D true;
=09=09=09=09receive.Start ();
=09=09=09catch (Exception e)
=09=09=09=09receive.Abort ();
=09=09=09=09reader.Close ();
=09=09=09=09connected =3D false;
=09=09=09=09sbupdate ("Error logging in");