Can C # compare with the legacy old VB6 DoEvents?

After reading for a long time, I just cannot compare it to DoEvents combined with a loop in legacy VB6 language. Do I think this is correct?

This is just to get an idea of ​​how it works, and I felt it could be a nice and simple explanation (not technically accurate of course).

Thanks for your data!

+3


source to share


3 answers


In some ways, they may seem similar at first glance. DoEvents

will run the nested message loop so that other methods can run on this thread before the current method completes. Likewise, await

will "give" control to other methods, allowing other code to start this thread before the current method ends.

However, there is a very important, very important distinction. DoEvents

saves the current call stack; when it calls other methods, it does so directly; this causes serious spawn problems and is the root cause of the often repeated "DoEvents are evil" phrase. On the contrary, it await

returns completely, so there is no direct re-entry.



Also, it DoEvents

is similar await

when used on the UI thread. await

can also be used in many other contexts.

+3


source


No, they are not the same. DoEvents

had nothing to do with multithreading, it just processed the window messages that were sitting in the message queue to make it look like your gui is responsive. This processing always took place in a thread called DoEvents

.



+2


source


May be. Doevents enables VB6 concurrent multithreading, decoupling processor multithreading (like Win 16 multitasking). You have all of the CPU multithreaded processing timing issues, but they can be solved with normal switching control programming (so no instruction is swapped out halfway through), unlike CPU timing where special CPU instructions have to be used. The CPU, because you are not using its multithreading, treats you as one thread (as it does for the CPU).

DoEvents is Win16 compatibility. It should not be used, it is dangerous and people use it to solve imaginary problems. THESE ARE THE CAUSES OF THE ERROR.

What does he do. It intercepts your function and jumps to the VB6 runtime to clear the message queue, making your procedure rerun and changing the values ​​your function was using for something else. Then it calls WinAPI sleep(0)

for Windows to send messages to all programs. Then BELOW may happen.

The only thing useful for this is to make the form update before the function completes.

But this is so dangerous.

+1


source







All Articles