Is Application.DoEvents() a form of Multitasking?

穿精又带淫゛_ 提交于 2019-12-13 03:05:43

问题


I am pretty sure Applicataion.DoEvents() in Windows Forms. is a very early, very primitive, WindowsForms only form of Multitasking. It has all the telltales and mechanics:

  • Pausing execution of the calling Event.
  • Making the rest of said Event a continuation to be run later.
  • Allowing the other Events/Processes to run. Just with some extra issues, because the MT is implemented via the EventQueue. Possibly even a recursive call to the Queue.

But I just ran into a person that insists it has "nothing to do with Multitasking", which I cannot reconcile with my understanding of the Function or the of Multitasking.

Note: I explicitly consider Mutltithreading only an implementation for Multitasking. It is clear that DoEvents() is not a form of Multithreading, as we all know how poorly that one works in GUI Environments.


回答1:


I am pretty sure it is a very early, very primitive, Windows Forms only form of Multitasking

You are pretty close to correct on all counts except for your conjecture that it is for WinForms only. "DoEvents" precedes WinForms; it was present in Visual Basic long before WinForms was invented, and "pump the message queue" obviously precedes VB also. And it was a bad idea and easily abused then too.

Making the rest of said Event a continuation to be run later.

DoEvents doesn't really make anything into a continuation the way that say, await does. Whatever event is currently "in flight" when DoEvents is called has its state on the stack, and the stack is the implementation of continuation in this case. This is another point against DoEvents -- unlike await, it eats stack, and therefore can contribute to an overflow.

I just ran into a poster that insists it has "nothing to do with Multitasking".

You should ask the author for clarification then, since that certainly sounds wrong.




回答2:


Well, it's called Preemptive Multitasking, meaning "interrupting a task". You do multiple Tasks, but never two at the same time. It's not about using multiple cores of the CPU, but a way to control multiple activities inside your program. Common Sample is, to give the program a chance to handle mouse movement by the user, while doing a lengthy operation, running something that can be considered a "batch"-job.

Normally you don't have to care about this "DoEvents", but if you know, you have a procedure running for more than 1 second, you should call it manually, you pass the control to another method thereby, you stop your own code, let other code run, and than you continue with your own code. So it's never asynchronous, but still some kind of "multitasking".

It's more a control structure, the important thing is, you do not know what's going on inside, you call it "just for case" - somebody else might need the CPU for a millisecond.

There is no external task scheduler interrupting your code and doing a context switch, you have to "behave" by interrupting your code yourself, if you do something lengthy. It is a convention that you do only "small" things in event handlers and return the control to Windows as soon as possible,either by finishing the method, or by calling DoEvents.




回答3:


DoEvents is not related to real multitasking nor multithreading. Using it appears for the user as a pseudo-multitasking. It allows to dispatch forms events like keys and mouse and paint and so on. So it allows to have a responsive GUI within the UI thread while doing a processing without using threads. It allows for exemple to do loops while permitting controls to be refreshed and user to act on some controls like a cancel button. You call DoEvents and the Application event handler dispatch events that are in single UI thread executed, then it returns to the caller. It is as simple as that to do simple things without boring about implementing multithreading and synchronize UI thread or manage locks. DoEvents only do a loop do clear the windows events queue.

https://referencesource.microsoft.com/system.windows.forms/R/0880978c39425ff1.html

Real multitasking is multiprocessing and multithreading. It requires a CPU supporting multitask. WIthout a such CPU, pseudo-multitask is first based on hardware interrupts and next on software processus ordonnancement and next on dispatching system events. But it is not real multitasking.

Multithreading is inside a processus. Multiprocessing is for processus. They are both multitasking. A processus is the application launched and it can have some threads. Multitasking is the capacity of the cpu to execute several instructions at the same time. For example Windows 10 on modern Intels supports both multiprocessing and multithreading. DoEvents has nothing to do with these things : it is related to an event management pattern to have a responsive GUI. This pattern was created earlier when there were no multitask cpu. First multitask processors were multi-processors but it's expensive and difficult to program. So the concept of core was created to have a good compromise.

https://en.wikipedia.org/wiki/Computer_multitasking

https://whatis.techtarget.com/definition/true-multitasking

https://unboxingtreatment.com/2017/09/quickbite-cpu-multitasking.html



来源:https://stackoverflow.com/questions/58472676/is-application-doevents-a-form-of-multitasking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!