Hi,
Attached is a very little Gtk# example which uses a Thread to execute
the process 'ping 127.0.0.1 -c 7' and refresh both the standard output and
a TextView.
It can be compiled with mcs or gmcs with
$ mcs -pkg:gtk-sharp-2.0 test.cs
The execution of the program shows different outputs in the TextView and
the console.
It seems like the string 'tmp' forgets its last values inside the delegate.
I use mono and mcs version 1.1.9.2 and Gtk# version 2.3.92 over Debian Sid.
What's the reason of this strange behavior?
Regards,
--
Antonio MartÃnez Alvarez
using System;
using System.IO;
using System.Diagnostics;
using System.Threading;
using Gtk;
class App
{
public static void Main()
{
new test_threads();
}
}
class test_threads
{
Process p;
ProcessStartInfo psi;
Window win = null;
TextView textview = null;
static TextBuffer buff = null;
static string tmp; // string read in ThreadFunc
public test_threads()
{
Application.Init ();
win = new Window ("Test");
win.SetDefaultSize (500, 300);
win.DeleteEvent += new DeleteEventHandler (delegate {Application.Quit();});
VBox box = new VBox (false, 0);
Button btn = new Button ("ping 127.0.0.1 -c 7");
btn.Clicked += new EventHandler (btn_click);
box.PackStart (btn, true, true, 1);
Button btn2 = new Button ("Clear TextView");
btn2.Clicked += delegate {buff.Text = "";};
box.PackStart (btn2, true, true, 1);
ScrolledWindow sw = new ScrolledWindow ();
textview = new TextView();
sw.Add(textview);
box.PackStart (sw, true, true, 1);
buff = textview.Buffer;
win.Add(box);
win.ShowAll ();
Application.Run();
}
void btn_click (object obj, EventArgs args)
{
psi = new ProcessStartInfo();
psi.FileName = "ping";
psi.Arguments = "127.0.0.1 -c 7";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
p = Process.Start(psi);
Thread thr = new Thread (new ThreadStart (ThreadFunc));
thr.Start ();
}
public void ThreadFunc()
{
while ((tmp = p.StandardOutput.ReadLine()) != null)
{
Console.WriteLine (tmp);
Application.Invoke (delegate
{
buff.Text += System.Environment.NewLine + tmp;
}
);
}
}
}
_______________________________________________
Gtk-sharp-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list