I have some questions about how multitasking works with events.
1. I know that in the o/s, the program that manages the GUI interface, input and manipulation from the user, etc, runs in its own thread/process so that it can be pre-emptively multitasked along with any other programs running on the computer. This is so that the GUI remains responsive and the user can do things such as move or resize windows and click buttons even when other programs are doing stuff. Is that correct and does it apply to all three platforms?
2. When I add a hook function to trap events from the GUI, ie in MaxGUI, anything that my hook does is being called from within the GUI's thread/process as if part of that thread/process, and is therefore multitasking alongside any `main program loop` I might have in my program and competing with it for time. So my hook function and my main code loop are essentially running as two separate threads that pre-empt each other. Is that a correct description of what is happening?
3. To avoid one part of your program clashing with another part in this way, ie a main program loop being too CPU-hungry and causing the GUI to slow down because there isn't enough time left for the GUI thread, you should get rid of the main loop as much as possible (have a WaitEvent() only), and put all main loop stuff into some other event hook such as the TIMERTICK or GADGETPAINT, so that the whole program is run from events. Is that a correct description/concept?
4. Because the hook function is called `as part of` the o/s's GUI process, the o/s doesn't put any more events into OUR event queue until we return from our hook function. In other words, events don't pre-empt other events in our queue, right? So we should be careful not to spend too much time in a given event if we're waiting for another more important event like a TIMERTICK?
5. Since the hook is called from the GUI thread, and is multitasking separately from the rest of our program, so long as we return from the hook soon enough, we could set up timers for each `virtual thread` that we could use as our own way of switching between tasks and allocating timeslices. Then whenever a TIMERTICK happens we can decide what timer it came from and then choose a function to call based on that, ie task switching. So events let us implement a rudimentary kind of multitasking within our app, right?
6. Since we would be multitasking parts of the app, we'd have to be careful of what else our app might be doing in a main loop because it would've been pre-empted and we can't be certain that current state will mess up if we touch it in the middle of something. But if we implemented our own `thread management` or semaphores/locks type of thing, we could fairly `safely` do pretty much anything in the hook using any other functions/variables that are part of the program?
7. Since the hook program uses the same variables and functions as all the rest of our program, all of that stuff including all data, objects, graphics etc, are all shared openly between the hook and the main program right?
8. In summary, you can use events to trigger custom `threads` of execution based on timed events, so long as you ideally can put a given thread on `hold` by returning from the hook at the end of its allowed timeslice and resume with it the next time around?
Any other issues or faults with this that you can see?
1. I know that in the o/s, the program that manages the GUI interface, input and manipulation from the user, etc, runs in its own thread/process so that it can be pre-emptively multitasked along with any other programs running on the computer. This is so that the GUI remains responsive and the user can do things such as move or resize windows and click buttons even when other programs are doing stuff. Is that correct and does it apply to all three platforms?
2. When I add a hook function to trap events from the GUI, ie in MaxGUI, anything that my hook does is being called from within the GUI's thread/process as if part of that thread/process, and is therefore multitasking alongside any `main program loop` I might have in my program and competing with it for time. So my hook function and my main code loop are essentially running as two separate threads that pre-empt each other. Is that a correct description of what is happening?
3. To avoid one part of your program clashing with another part in this way, ie a main program loop being too CPU-hungry and causing the GUI to slow down because there isn't enough time left for the GUI thread, you should get rid of the main loop as much as possible (have a WaitEvent() only), and put all main loop stuff into some other event hook such as the TIMERTICK or GADGETPAINT, so that the whole program is run from events. Is that a correct description/concept?
4. Because the hook function is called `as part of` the o/s's GUI process, the o/s doesn't put any more events into OUR event queue until we return from our hook function. In other words, events don't pre-empt other events in our queue, right? So we should be careful not to spend too much time in a given event if we're waiting for another more important event like a TIMERTICK?
5. Since the hook is called from the GUI thread, and is multitasking separately from the rest of our program, so long as we return from the hook soon enough, we could set up timers for each `virtual thread` that we could use as our own way of switching between tasks and allocating timeslices. Then whenever a TIMERTICK happens we can decide what timer it came from and then choose a function to call based on that, ie task switching. So events let us implement a rudimentary kind of multitasking within our app, right?
6. Since we would be multitasking parts of the app, we'd have to be careful of what else our app might be doing in a main loop because it would've been pre-empted and we can't be certain that current state will mess up if we touch it in the middle of something. But if we implemented our own `thread management` or semaphores/locks type of thing, we could fairly `safely` do pretty much anything in the hook using any other functions/variables that are part of the program?
7. Since the hook program uses the same variables and functions as all the rest of our program, all of that stuff including all data, objects, graphics etc, are all shared openly between the hook and the main program right?
8. In summary, you can use events to trigger custom `threads` of execution based on timed events, so long as you ideally can put a given thread on `hold` by returning from the hook at the end of its allowed timeslice and resume with it the next time around?
Any other issues or faults with this that you can see?