views:

74

answers:

2

I'm updating legacy code, written in VB6, and I've come across the need for a mutex. I have two sockets, and I need to send and receive from various sources. So I plan on having one socket continuously listening for incoming connections, then the other is used to send or receive.

A timer checks twenty times a second if a connection has come in, and if so, uses the second socket to accept it, then begins listening again immediately. If a second connection comes in before the first has finished receiving data, it needs to wait (in C# I'd throw a lock on it and call it done.)

Also, if the program needs to send data, I'll use the second socket, and connect to the remote host. So if the second sockets already receiving data, it needs to block as well.

I'm not familiar with multithreading in VB6 - is this a problem, or is the timer's Tick event always raised on the same thread as everything else?

+4  A: 

The Tick event is always raised on the same single thread. Everything will automatically block because there's only one thread.

Multithreading in VB6 doesn't really work in my opinion, apart from using an ActiveX EXE project: there are various hacks to get multithreading working but they sound foul to me, though other people swear by them.

MarkJ
+3  A: 

If you use the standard VB6 winsock control, you don't have to even worry about using a timer, events will fire when a connection is made (and yes, that is on the main thread). The only thing you have to be very careful about for "locks" is you can get into a bad situation if you have DoEvents in your code as this does pump the message loop and you can have code from other functions running in the middle of the function that calls DoEvents. If you need DoEvents to keep your program "responsive", however you can use simple Booleans as locks, as VB6 is not Multithreaded. And MarkJ is correct, you have to go very far out of your way and do very sketchy things to get VB6 to be Multithreaded without using an ActiveX EXE project.

Kris Erickson