How to invoke a function in main loop context