What was the VB6 thread pattern?

I am porting an old VB6 program to C # /. Net. I am not very good at VB6 and I am asking for a better understanding.

The old VB6 program had basic program flow, but it also had a lot of event handlers for either socket events or timer events, and handlers for these commonly managed shared resources, shared globals when they woke up and ran.

However, the old program seemed to work fine.

Trying to do this same architecture in C # is disastrous because event handlers for the socket or timers call the system in different threads from the main application thread and result in frequent exceptions like "the calling thread cannot access this object because it owns a different topic." ... Not to mention the more subtle issues. Most of my work in conversion is re-archiving the program to make it thread-safe and eliminating the originally used global variable.

My question . VB6 event handlers run on separate threads? If so, how does VB6 ever get away with it? Among other things, the VB6 program had a timer that woke up every 4 seconds, manipulated some global variables, and fell asleep again while the main program was doing its job. I do not understand why this did not result in collisions.

+3


source to share


3 answers


Thread-Threading Threading in Visual Basic

If you need detailed details, explore the flat thread models in COM. VB6 mainly uses COM and built-in threading implication models to treat individual threads as message-passing objects. This simplifies thread safety, but underneath you, you are sacrificing a lot of overhead by basically treating all method calls as queued calls.

All of your code basically runs in a container that implements COM service calls. If you've ever worked with something written in VB6 in another language, you can interact with them through COM, usually.

Do VB6 event handlers run on separate threads?



Not really, because there are no separate threads. Your code runs in a single thread, terminated in the service-like architecture described above. Most of what you are talking about with this is other COM objects that have their own apartments. So to communicate back and forth, you basically make RPC calls when the threads are talking to each other: you are not manipulating them directly.

Among other things, the VB6 program had a timer that woke up every 4 seconds, manipulated some global variables and fell asleep again, while the main program did its job. I do not understand why this did not result in collisions.

The "timer" is on a separate thread created for the timer, but when it calls your code, you are guaranteed not to interrupt any other functions because function calls are basically queued one at a time in the thread.

+5


source


My question is, do VB6 event handlers run on separate threads?

General answer: No.

Real Answer: Yes, if you make nasty Win32 calls to create more threads. The chances of doing it right are close to zero. I don't think I've ever seen it in real code.



Among other things, the VB6 program had a timer that woke up every 4 seconds, manipulated some global variables and fell asleep again, while the main program did its job. I do not understand why this did not result in collisions.

When the timer wakes up, it puts a message on the UI queue. When the UI goes idle, it processes the message and fires an event on the UI thread.

You can do the same in WinForms if you use the correct timer.

+7


source


Matt Wilko, DoEvents is a VB6 that implements virtual co-threading. What a thread, something that can be confused to run other code. When you use DoEvents, you interrupt your code execution, when the processor is multithreading, it preempts your code. The effect is the same. One is done with the VB6 runtime and the other with the processor.

The effect is the same. YOU NEED TO SYNCHRONIZE ACCESS TO GLOBAL OBJECTS AND VARIABLES.

Your event handlers shouldn't take too long. Each event is queued and fired in turn.

Also the VB6 multithreading way is to put other things in a COM Exe file with an asynchronous model. Excel calls back when done.

0


source







All Articles