Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Created November 18, 2011 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kedrigern/1377159 to your computer and use it in GitHub Desktop.
Save Kedrigern/1377159 to your computer and use it in GitHub Desktop.
Threads oriented gui and concept of programm. Small example.
using System;
using System.Threading;
using Gtk;
namespace koncept
{
public static class MainClass
{
/* locks */
public static object lockEnd;
public static object lockM;
/* data exchange */
public static bool end;
public static string message;
/* threads */
static Thread t2;
public static void Main ()
{
end = false;
message = "Ahoj světě! Napiště něco!";
lockEnd = new object();
lockM = new object();
t2 = new Thread( Logic.run );
/* run */
t2.Start();
Design.run();
t2.Join();
/* */
}
}
public static class Logic
{
private static string mes;
public static void run() {
Console.WriteLine("Napište:");
while(true) {
lock( MainClass.lockEnd ) {
if( MainClass.end ) break;
}
mes = Console.ReadLine();
Console.WriteLine("Přečetl jsem: {0}", mes);
if( mes == "end" ) break;
if( mes != "" ) Gtk.Application.Invoke( DoRefresh );
Thread.Sleep( 500 );
}
/* ending sequence */
Gtk.Application.Invoke( DoExit );
Console.WriteLine("Ukončuji tento thread.");
}
private static void DoExit(object sender, EventArgs e) {
lock( MainClass.lockEnd ) {
MainClass.end = true;
}
}
private static void DoRefresh(object sender, EventArgs e) {
lock ( MainClass.lockM ) {
MainClass.message = mes;
}
}
}
public static class Design
{
static AppWin aw;
public static void run() {
Gtk.Application.Init();
aw = new AppWin();
aw.Show();
while( Gtk.Application.EventsPending() ) {
Gdk.Threads.Enter ();
Gtk.Application.RunIteration();
aw.Change( MainClass.message );
Gdk.Threads.Leave ();
lock( MainClass.lockEnd) {
if( MainClass.end ) break;
}
}
}
}
public class AppWin : Gtk.Window
{
Label l;
public AppWin() : base( WindowType.Toplevel ) {
this.DefaultWidth = 200;
this.DefaultHeight = 100;
this.l = new Label("Piš!");
this.Add( l );
this.ShowAll();
this.DeleteEvent += OnDeleteEvent;
}
public void Change( string m ) {
lock( MainClass.lockM ) {
l.Text = m;
}
l.ShowNow();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
((AppWin) sender).Hide();
lock( MainClass.lockEnd ) {
MainClass.end = true;
}
a.RetVal = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment