[Gtk-sharp-list] How to show custom window and do the computation in the background

Christopher David Howie me at chrishowie.com
Fri Jun 25 16:20:10 EDT 2010

On 06/25/2010 03:56 PM, Maciej Pilichowski wrote:
> Thank you for this link, I see similar approach as for WPF. However I 
> still didn't do it right, I tried:
> 		Gtk.Application.Invoke (delegate{ dlg.Run(); });
> or dlg.Show(). Now neither of calls shows the dialog.
>   Just in case -- computation is loading a big file. Loading occurs 
> when user chooses a file in a file dialog. At start I show main 
> window and -- if user set argument for a program -- I set initial 
> filename for file dialog.
> --> show main window 
> --> create file dialog 
> --> set filename for file dialog 
> --> the above triggers loading a file 
> --> create progress dialog 
> --> show progress dialog (*) 
> --> load file

The Application.Invoke method will push an event on the queue that will
run the passed delegate once it reaches the top of the event queue.  If
you are calling Invoke from the GTK+ thread, then the delegate will
never run until control is returned to the GLib main loop.

Here is the modified logic you should use.  Changed lines are annotated
with +.

--> show main window
--> create file dialog
--> set filename for file dialog
+-> the above triggers the following method:
  --> create progress dialog
  +-> spawn thread, thread will:
    +-> load file, using Application.Invoke to change the progress bar
        on the created dialog
    +-> once done, Destroy() the dialog
  +-> show progress dialog, using dialog.Show()

Note the pattern: set up the GUI for the operation, do the work on
another thread, and update the GUI from that thread.  The mail thread
should be in the GLib main loop code as much as possible, since whenever
it is not processing the event queue the UI will be frozen.

Chris Howie

If you correspond with me on a regular basis, please read this document:

PGP fingerprint: 2B7A B280 8B12 21CC 260A DF65 6FCE 505A CF83 38F5


This document should be read only by those persons to whom it is
addressed.  If you have received this message it was obviously addressed
to you and therefore you can read it.

Additionally, by sending an email to ANY of my addresses or to ANY
mailing lists to which I am subscribed, whether intentionally or
accidentally, you are agreeing that I am "the intended recipient," and
that I may do whatever I wish with the contents of any message received
from you, unless a pre-existing agreement prohibits me from so doing.

This overrides any disclaimer or statement of confidentiality that may
be included on your message.

More information about the Gtk-sharp-list mailing list