BackgroundWorker and program not responding after a few seconds

I use a BackgroundWorker to do some intensive task in background and, in the graphics main thread, I have a popup sprite with a button allowing to cancel it. The problem is that, after a few second, the program is declared as “not responding” by the windows OS (not tested on android yet) and I can’t cancel the BackgroundWorker anymore (click events are no longer dispatched to the graphics thread)
If I slow down the BackgroundWorker by putting some trace output in my loop task for example, that solve the problem (windows do not consider the program as non responding anymore), but it is not a satisfactory solution (as it slow down my BackgroundWorker task ). I also tried to put some animated sprite in the graphics main thread to keep it up (like a “spinning wheel” with a sprite that rotate endlessly) but that did not work (the program is still declared as “not responding” after a few second and the spining wheel sprite stop rotating and freeze).

So how can I use BackgroundWorker to perform some intensive computation task in background without the program to freeze after a few seconds?

Try calling Sys.sleep(0) periodically. This will give the main thread a chance to run, and if the main thread doesn’t need to do anything, the worker will get back to work with no delay.

Disclaimer: I’ve barely worked with threads, and I’ve never worked with threads in Haxe.

1 Like

That works. Thank you very much.
Didn’t know sleep(0) would allow the main thread to get back into foreground (I naively thought it will require at least a sleep(0.000…0001) for the main thread to have a chance to take the lead)