> The problem is that the value of "tmp" is shared (captured) and as soon
> as you queue the delegate, tmp will be reloaded with the next value from
> ReadLine.
Thanks Miguel,
Now I have tried to use a little C program (attached) which uses printf
and sleep(1). The usage is 'foo 4' and prints 4 strings, one every second.
The situation is just the same, I can't understand why the command ping
works fine and my foo program not. The standard out and the TextView are
refreshed at the end of the execution of foo.
I've tried "\n", "\r", "\n\r" ... without success...
I've attached the 2 sources:
$ gcc foo.c -o foo
$ gmcs -pkg:gtk-sharp-2.0 test.cs
$ mono test.exe
What's wrong at this time?
Regards,
--
Antonio MartÃnez Alvarez
http://atc.ugr.es/~amartinez
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main (int argc, char **argv)
{
if (argc < 2)
printf ("Usage: foo <integer>\n");
else
{
int num = atoi (argv[1]);
int i;
for (i = 0; i < num; i++)
{
printf ("Iteration #(%i)\n", i+1);
sleep (1);
}
}
printf ("Program finished.\n");
return 0;
} 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 ("foo 10");
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 = "foo";
//psi.FileName = "ping";
psi.Arguments = "10";
//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)
{
string tmp2 = tmp;
Console.WriteLine (tmp);
Application.Invoke (tmp2, null, delegate
{
buff.Text += System.Environment.NewLine + tmp2;
}
);
}
}
}
_______________________________________________
Gtk-sharp-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list