First thing you have to know, is that you MUST compile your program with -threaded for it to work properly. Otherwise you will get random hangs.

You can save this tutorial as a file and compile with:

ghc gtk2hs-threaded.lhs -threaded

>import Control.Concurrent

You need forkIO and forkOS to do things properly.

>import Control.Concurrent.MVar

You'll also need MVars.

>import Graphics.UI.Gtk

You'll also want to be able to send your own exit signals.

>import System.Exit

>main ::IO()>main =do

>_<- initGUI

The first thing we do, is create an exit MVar. This MVar is going to keep our application alive until we wish to quit.

> exit <- newEmptyMVar

Now we can create a window with some buttons and some threads of our own. Watch out though, at least one button MUST be bound to filling the exit MVar, or else our program will close immediately and complain that it blocked indefinitely on an MVar operation.

> window <- windowNew

Our window will be very simple. It will show a single label, showing the time in seconds since the program started, and an exit button.

Events are run within GTK's own thread. There is no need for extra precautions when running event code.

> exitButton `on` buttonActivated
>$ putMVar exit ExitSuccess

Now we're going to make a thread that updates the time label every second.

> forkIO $do>let> printTime t =do{

So we wait one second.

> threadDelay 1000000;

And then we want to print that time to the label. We are not in GTK's own thread however. So we'll have to send GTK a drawing action(of type IO a). We can use one of two functions to do this. postGUIAsync, which simply runs the action inside GTK's thread, or postGUISync, which runs the action and then returns the result. Obviously Async is non blocking whereas Sync is blocking. If you forget to put postGUI(A)Sync, your program may crash randomly in horrible ways. Arguably, gtk drawing actions should be typed in a GTK monad so that the compiler would prevent such unfortunate runtime errors. However no one has bothered to do this(yet), so we end up suffering a bit sometimes.

This is the function that starts GTK's event loop in it's own thread. This must be forkOS and not forkIO as the GTK thread is a FFI thread and cannot take advantage of Haskell's "green thread" capabilities..

> forkOS mainGUI

When we want our program to exit, we will just fill this MVar with an exit signal.

> signal <- takeMVar exit

We rarely want to exit imediately. Usually, we want to wait for some other thread to finish saving the file/showing a dialog asking if the user wants to save. We should have appropriate waiting code here.

We also want to close GTK. Again, GTK is in another thread, so we have to post the action to the GTK thread: