[Gtk-sharp-list] Application.Invoke and threads: strangebehavior

Miguel de Icaza miguel at ximian.com
Wed Oct 19 08:42:06 EDT 2005


Hello,

> 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.

Because the variables are captured.  For details, you can read the C#
2.0 specification which explains it in detail.

Basically the variable is *shared* between the two threads, but
Application.Invoke is called with a timeout, so by the time it is called
the shared variables have already changed their meaning.

So you have a situation where you have shared data with no locking of
any kind.   Use a synchronized queue to pass data from one side to the
other for example instead of sharing the variable.


More information about the Gtk-sharp-list mailing list