Declaring and Raising Events in Visual Basic 6. This event handler works much like the Change event works for the TextBox control in VB6. The class contains a private field and a property to set and get the value of the private field. The property is named Text. Listing 1 contains the class before we add any event code to it. The Introduction to Multithreading video, as part of the Foundations of Programming course, covers the basics of multi-threading, when and how to use it, and challenges inherent with multithreading, including the lack of available resources. Watch this Lynda.com JavaScript tutorial to learn how to use multithreading to make programs responsive.
How to implement multi-threading in visual basic 6.0. It would be great if someone can give an example.
Daniel Imms
35.4k7 gold badges104 silver badges142 bronze badges
raviravi
3852 gold badges8 silver badges15 bronze badges
4 Answers
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:
petr k.petr k.
6,2636 gold badges36 silver badges52 bronze badges
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 SmithHamish Smith
7,6161 gold badge30 silver badges47 bronze badges
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 WhitledgeJeffrey L Whitledge
47.7k8 gold badges58 silver badges92 bronze badges
You can use the Interop Forms Toolkit 2.0 for multithreading in VB6. The Toolkit allows you to take advantage of .NET features without being forced onto an upgrade pat. Thus you can also use .NET User Controls as ActiveX controls in VB6.
user1947746user1947746
Not the answer you're looking for? Browse other questions tagged vb6 or ask your own question.
To understand threading in VB.NET, it helps to understand some of the foundation concepts. First up is that threading is something that happens because the operating system supports it. Microsoft Windows is a pre-emptive multitasking operating system. A part of Windows called the task scheduler parcels out processor time to all the running programs. These small chunks of processor time are called time slices. Programs aren't in charge of how much processor time they get, the task scheduler is. Because these time slices are so small, you get the illusion that the computer is doing several things at once.
Definition of Thread
A thread is a single sequential flow of control.
Some qualifiers:
- A thread is a 'path of execution' through that body of code.
- Threads share memory so they have to cooperate to produce the correct result.
- A thread has thread-specific data such as registers, a stack pointer, and a program counter.
- A process is a single body of code that can have many threads, but it has at least one and it has a single context (address space).
This is assembly level stuff, but that's what you get into when you start thinking about threads.
Multithreading vs. Multiprocessing
Multithreading is not the same as multicore parallel processing, but multithreading and multiprocessing do work together. Most PCs today have processors that have at least two cores, and ordinary home machines sometimes have up to eight cores. Each core is a separate processor, capable of running programs by itself. You get a performance boost when the OS assigns a different process to different cores. Using multiple threads and multiple processors for even greater performance is called thread-level parallelism.
A lot of what can be done depends on what the operating system and the processor hardware can do, not always what you can do in your program, and you shouldn't expect to be able to use multiple threads on everything. In fact, you might not find many problems that benefit from multiple threads. So, don't implement multithreading just because it's there. You can easily reduce your program's performance if it's not a good candidate for multithreading. Just as examples, video codecs may be the worst programs to multithread because the data is inherently serial. Server programs that handle web pages might be among the best because the different clients are inherently independent.
Practicing Thread Safety
Multithreaded code often requires complex coordination of threads. Subtle and difficult-to-find bugs are common because different threads often have to share the same data so data can be changed by one thread when another isn't expecting it. The general term for this problem is 'race condition.' In other words, the two threads can get into a 'race' to update the same data and the result can be different depending on which thread 'wins'. As a trivial example, suppose you're coding a loop:
If the loop counter 'I' unexpectedly misses the number 7 and goes from 6 to 8—but only some of the time—it would have disastrous effects on whatever the loop is doing. Preventing problems like this is called thread safety. If the program needs the result of one operation in a later operation, then it can be impossible to code parallel processes or threads to do it.
Basic Multithreading Operations
It's time to push this precautionary talk to the background and write some multithreading code. This article uses a Console Application for simplicity right now. If you want to follow along, start Visual Studio with a new Console Application project.
The primary namespace used by multithreading is the System.Threading namespace and the Thread class will create, start, and stop new threads. In the example below, notice that TestMultiThreading is a delegate. That is, you have to use the name of a method that the Thread method can call.
In this app, we could have executed the second Sub by simply calling it:
![Vb6 multithreading class 10 Vb6 multithreading class 10](/uploads/1/2/4/8/124874093/658615693.jpg)
This would have executed the entire application in serial fashion. The first code example above, however, kicks off the TestMultiThreading subroutine and then continues.
A Recursive Algorithm Example
Here's a multithreaded application involving calculating permutations of an array using a recursive algorithm. Not all of the code is shown here. The array of characters being permuted is simply '1,' '2,' '3,' '4,' and '5.' Here's the pertinent part of the code.
Notice that there are two ways to call the Permute sub (both commented out in the code above). One kicks off a thread and the other calls it directly. If you call it directly, you get:
However, if you kick off a thread and Start the Permute sub instead, you get:
This clearly shows that at least one permutation is generated, then the Main sub moves ahead and finishes, displaying 'Finished Main,' while the rest of the permutations are being generated. Since the display comes from a second sub called by the Permute sub, you know that is part of the new thread as well. This illustrates the concept that a thread is 'a path of execution' as mentioned earlier.
Race Condition Example
The first part of this article mentioned a race condition. Here's an example that shows it directly:
The Immediate window showed this result in one trial. Other trials were different. That's the essence of a race condition.