| 1 |
-- Example of concurrent Haskell and Gtk. |
| 2 |
-- |
| 3 |
-- As long as GHC does not support OS level threads by default, a trick has |
| 4 |
-- to be used to let Haskell programs continue while the GUI is running. |
| 5 |
-- We attach a call to the Haskell function "yield" to the timeout handler of |
| 6 |
-- Gtk's main loop. Thus GHC regularly gets a chance to execute Haskell |
| 7 |
-- threads. |
| 8 |
|
| 9 |
import Graphics.UI.Gtk |
| 10 |
|
| 11 |
import Control.Applicative |
| 12 |
import Prelude |
| 13 |
import Control.Concurrent |
| 14 |
|
| 15 |
main :: IO () |
| 16 |
main = do |
| 17 |
initGUI |
| 18 |
dia <- dialogNew |
| 19 |
dialogAddButton dia stockClose ResponseClose |
| 20 |
contain <- castToBox <$> dialogGetContentArea dia |
| 21 |
pb <- progressBarNew |
| 22 |
boxPackStart contain pb PackNatural 0 |
| 23 |
widgetShowAll dia |
| 24 |
forkIO (doTask pb) |
| 25 |
|
| 26 |
-- 50ms timeout, so GHC will get a chance to scheule about 20 times a second |
| 27 |
-- which gives reasonable latency without the polling generating too much |
| 28 |
-- cpu load. |
| 29 |
timeoutAddFull (yield >> return True) priorityDefaultIdle 50 |
| 30 |
dialogRun dia |
| 31 |
return () |
| 32 |
|
| 33 |
doTask :: ProgressBar -> IO () |
| 34 |
doTask pb = do |
| 35 |
progressBarPulse pb |
| 36 |
threadDelay 100000 |
| 37 |
doTask pb |