1-- Example of concurrent Haskell and Gtk. 2-- 3-- This demo uses GHC's support for OS level threads. It has to be 4-- linked using the ghc -threaded flag. 5-- 6-- Because Gtk+ is single threaded we have to be very careful to call 7-- Gtk+ only from the main GUI thread. So while it's ok to forkIO, 8-- any GUI actions in that thread have to be 'posted' to the main GUI 9-- thread using postGUI, or postGUIAsync as in the example here. 10 11import Graphics.UI.Gtk 12 13import Control.Applicative 14import Prelude 15import Control.Concurrent 16 17main :: IO () 18main = do 19 20 -- It is marked unsafe becuase it is your obligation to ensure you 21 -- only call Gtk+ from one OS thread, or 'bad things' will happen. 22 unsafeInitGUIForThreadedRTS 23 24 dia <- dialogNew 25 dialogAddButton dia stockClose ResponseClose 26 contain <- castToBox <$> dialogGetContentArea dia 27 pb <- progressBarNew 28 boxPackStart contain pb PackNatural 0 29 widgetShowAll dia 30 forkIO (doTask pb) 31 32 dialogRun dia 33 return () 34 35doTask :: ProgressBar -> IO () 36doTask pb = do 37 postGUIAsync $ progressBarPulse pb 38 threadDelay 100000 39 doTask pb 40