tags:

views:

1767

answers:

4

How to implement multi-threading in visual basic 6.0. It would be great if someone can give an example.

+6  A: 

VB6 is not a really good environment for multi-threaded applications. There is no out-of-the-box support, you need to delve into standard WinAPI functions. Take a look at this article, which provides quite a comprehensive sample:

http://www.freevbcode.com/ShowCode.Asp?ID=1287

petr k.
+3  A: 

On several projects I have implemented asynchronous processing in VB6 using multiple processes. Basically having a worker thread within an active exe project that is separate from the main process. The worker exe can then be passed whatever data it needs and started, raising back an event to say it's finished or there is data for the main process.

It's a more resource hungry (an extra process rather than a thread) but VB6 is running in a single threaded apartment and doesn't have any built in support for starting new threads.

If you really need to have multiple threads within one process I'd suggest looking at using .net or VC6 rather than VB6.

Hamish Smith
+1  A: 

Check out this article by Scott Swigart. It talks about how to use a COM Interop wrapper that allows you to use .NET's BackgroundWorker component in VB 6.0.

Using Background Threads with Visual Basic 6

Rob Windsor
A: 

If the problem that you are trying to solve is a long calculation and you want to keep the UI responsive, then one possibility is to frequently call the DoEvents function within your long calculation. This way, your program can process any Windows messages, and thus the UI will respond to user commands. You can also set up a Cancel button to signal your process that it needs to end.

If you do this, then you will need to be careful to disable any controls that could cause a problem, such as running the long process a second time after it has started.

Jeffrey L Whitledge