1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
//
// Thread.cs - Using threads with Gtk#
//
// Author: Miguel de Icaza
//
// (c) 2005 Novell, Inc.
namespace GtkSamples {
using Gtk;
using Gdk;
using System;
using System.Threading;
public class HelloThreads {
static Label msg;
static Button but;
static int count;
static Thread thr;
public static int Main (string[] args)
{
Application.Init ();
Gtk.Window win = new Gtk.Window ("Gtk# Hello World");
win.DeleteEvent += new DeleteEventHandler (Window_Delete);
msg = new Label ("Click to quit");
but = new Button (msg);
but.Clicked += delegate { thr.Abort (); Application.Quit (); };
win.Add (but);
win.ShowAll ();
thr = new Thread (ThreadMethod);
thr.Start ();
Application.Run ();
return 0;
}
static void ThreadMethod ()
{
Console.WriteLine ("Starting thread");
while (true){
count++;
Thread.Sleep (500);
Application.Invoke (delegate {
msg.Text = String.Format ("Click to Quit ({0})", count);
});
}
}
static void Window_Delete (object obj, DeleteEventArgs args)
{
Application.Quit ();
args.RetVal = true;
}
}
}
|