Search code examples
multithreadingmonodevelopgtk#

How to set textview from another thread


I'm trying to set a textview's text from other thread than the main one,so I've written in the constructor:

    Thread myth = new Thread (new ThreadStart (set_txt));
    myth.Start ();

and of course set_txt is a method contains

    textview1.Buffer.Text = "Whatever";

The probleme is that When I run the code most time it stops and gives an error:

    =================================================================
    Got a SIGSEGV while executing native code. This usually indicates
    a fatal error in the mono runtime or one of the native libraries 
    used by your application.
    =================================================================

What should I do??


Solution

  • You need to update the GTK# text view from the GUI thread. You can do this by using Gtk.Application.Invoke:

     Gtk.Application.Invoke (delegate {
         textview1.Buffer.Text = "Whatever";
     });