Version française
Home     About     Download     Resources     Contact us    
Browse thread
Why 'Graphics.wait_next_event' doesn't reply anymore ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Fabrice Marchant <fabrice.marchant@o...>
Subject: Why 'Graphics.wait_next_event' doesn't reply anymore ?
  Hi !

  This issue with 'Graphics.wait_next_event' seems only to happen in some cases with threads.
Please consider the code snippet below :
---------------------------------------------------
let id = ref None

let run () =
  while !id <> None do
    step ();  (* Changes drawing state *)
    plot () (* ;
    Thread.delay 0.01 *)
  done

let rec test () =
  print "Graphics.wait_next_event, next statement...\n"; (* A flushed printf *)
  let status = wait_next_event [ Key_pressed ] in
  print "Graphics.wait_next_event returned.\n";
  match status.key with
    'r' ->
      (match !id with
        None -> id := Some (Thread.create run ())
      | Some _ -> ());
      test ()
  | 's' ->
      id := None;
      test ()
  | _ ->
      ()
-----------------------------------------------
  First time you press 'r', a thread is created. ( There is no delay problem in changing thread 'id' after creation and process test '!id <> None' is soon true at first call. )
  So the thread does its animation job. Here is the full short - stuck - test code :
http://fabrice.marchant.free.fr/code/wait_next_event/
After the thread is launched, we can check the console to state that the "main" thread is waiting for a key press.

The issue arises now.
 You can hit any key : 'Graphics.wait_next_event' couldn't care less.

Please why ?

  Another thing I do not understand :
if you insert a delay inside the 'run' thread code loop ( please see uncommentable Thread.delay above ), the program does not stick anymore, 'Graphics.wait_next_event' normally listening to the keyboard.
( Nice, but my aim was to run this thread at full speed... )

Thanks,

Fabrice