[Mono-winforms-list] Updating GUI from an asynchronous thread

Ian Norton inorton at gmail.com
Wed Apr 3 09:59:31 UTC 2013


Reading your program, it looks like you only run the gtk main loop when
your child gives you data.

Also, that is gtk, this is the win forms list :-)
On 2 Apr 2013 21:18, "Neil Thackeray" <neilt at illinois.edu> wrote:

> I'm having trouble getting the output from a command line program to
> appear in the main window of a form. For simplicity I'm just trying to get
> it to appear in a label for now. When I run the function RunIt nothing from
> stdout of the child process appears in the label, but ConsoleWriteline
> seems to pick it up just fine. When it gets down to the while loop it
> updates the label perfectly fine.
>
> I've also noticed that the form seems to be locked while the child process
> is running. When I was just using BackgroundWorker without
> Application.Invoke the form updated from the child process and the while
> loop, but the update was spotty and the application crashed sporadically.
>
> Any suggestions are appreciated.
>
> Thanks
>
> namespace windowtest
> {
> public partial class MainWindow: Gtk.Window
> {
>     public MainWindow (): base (Gtk.WindowType.Toplevel)
>     {
>          Build ();
>     }
>
>     protected void OnDeleteEvent (object sender, DeleteEventArgs a)
>     {
>         Application.Quit ();
>         a.RetVal = true;
>     }
>
>         public void RunIt ()
>         {
>             string prog = @"C:/Program Files (x86)/Program/Program.exe";
>             // Start the child process.
>             System.Diagnostics.Process p = new
> System.Diagnostics.Process();
>             p.StartInfo.UseShellExecute = false;
>             p.StartInfo.Arguments = "Program Arguments";
>             p.StartInfo.**RedirectStandardOutput = true;
>             p.StartInfo.FileName = prog;
>             p.StartInfo.CreateNoWindow = true;
>             p.EnableRaisingEvents = true;
>             // Redirect the output stream of the child process.
>             p.OutputDataReceived += new DataReceivedEventHandler(
>                 delegate(object sender, DataReceivedEventArgs e)
>                 {
>                     if (!String.IsNullOrEmpty(e.Data)**)
>                     {
>                         lblTest.Text = e.Data;
>                         while (Gtk.Application.EventsPending ())
>                             Gtk.Application.RunIteration ();
>                         Console.WriteLine(e.Data);
>                     }
>                 }
>             );
>             p.Start();
>             p.BeginOutputReadLine();
>             p.WaitForExit();
>             p.Close();
>             int i = 0;
>             while (i < 20)
>             {
>                 lblTest.Text = i.ToString();
>                 while (Gtk.Application.EventsPending ())
>                     Gtk.Application.RunIteration ();
>                 Console.WriteLine(i.ToString()**);
>                 i++;
>                 System.Threading.Thread.Sleep (100);
>             }
>             lblTest.Text = "Test";
>         }
>
>         protected void OnButton4Clicked (object sender, EventArgs e)
>         {
>             System.ComponentModel.**BackgroundWorker bw = new
> System.ComponentModel.**BackgroundWorker();
>             bw.WorkerSupportsCancellation = true;
>             bw.DoWork += new System.ComponentModel.**
> DoWorkEventHandler(TestInvoke)**;
>             if (bw.IsBusy != true)
>             {
>                 bw.RunWorkerAsync();
>             }
>         }
>         private void TestInvoke(object sender, System.ComponentModel.**DoWorkEventArgs
> e)
>         {
>             Application.Invoke(delegate{ RunIt(); });
>         }
> }
> }
> ______________________________**_________________
> Mono-winforms-list maillist  -  Mono-winforms-list at lists.**ximian.com<Mono-winforms-list at lists.ximian.com>
> http://lists.ximian.com/**mailman/listinfo/mono-**winforms-list<http://lists.ximian.com/mailman/listinfo/mono-winforms-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20130403/094dbe27/attachment.html>


More information about the Mono-winforms-list mailing list