Hi Eli, Threads seem to the correct solution for the problem I am trying to solve. Thank you for your help. Thanks, Ivan On Fri, May 8, 2020 at 2:45 PM Eli Zaretskii wrote: > > From: Ivan Yonchovski > > Cc: emacs-devel@gnu.org > > Date: Fri, 08 May 2020 14:23:34 +0300 > > > > (defun process-events () > > (when (input-pending-p) > > (run-with-timer 0.01 nil (lambda () (throw 'exit nil))) > > (recursive-edit))) > > > > (dotimes (counter 1000) > > (sleep-for 0.01) ;; simulate execution of N tasks > > (message "%s" counter) > > (process-events)) > > > > This code is able to handle C-n but obviously it will break if someone > > starts another recursive-edit or for complex keybindings, e. g. `C-x b`. > > I think you will indeed find that this is very fragile. > > > Are you aware of something doing something similar? Do you think it > > makes sense if you want to process something slow on the background > > without blocking emacs? > > I think this is why we added threads to Emacs. Why not try doing this > in a thread? > > > > AFAIU, such facilities need to have a separate event loop that > > > generally runs in a separate thread. > > > > In the C# case it all happens on the UI thread. DoEvents means handle > > mouse clicks, repaint controls, etc(the stuff from the event loop) and > > then continue with current execution. > > When display is as intimately connected to the global state as it is > in Emacs, I don't think such separation can fly. >