Multithreading

Hi, welcome to this little tuturial on Win32 multithreading in Visual Basic.

For more mutithreading examples download the .zip file which includes a full sample project and my clsThreading.cls which makes multithreading much easier.

This tutorial is also included in the .zip file so you donīt have to read it here.

1. What is multitasking?

On Windows, as it is a 32 bit operating system, more then one task can run at once. Everybody knows that, you can e.g. run Paint and Windows Notepad at the same time.

You can switch between these tasks using the buttons in the taskbar.Well, they do not really run at the same time, because only one app can control the CPU at once, but Windows switches the processor control between these apps very fast, so that it seems that they are running at the same time.

This ability of Windows to handle various tasks at once is called multitasking.

 

2. What is multithreading?

But Windows can do even more.

Not only various tasks can run at once, but one task can create multiple threads, where every thread has its own function. For example, Windows Explorer can copy a huge file (with the file copy dialog) and, at the same time, you can still use the TreeView to navigate through the folders. A normal Visual Basic app is disabled until a task is finished (e.g. open a big file). In a multithreaded program, you can also click the titlebar and this wonīt stop the programīs activities.

The ability of Windows to allow one app to handle multiple threads is called multithreading.

 

3. How can I implement multithreading in my VB program?

To use the cool multithreading in your VB app, you need some API calls (or my clsThreading.cls which is included in the .zip file) :

The first and most important is the CreateThread call :

Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long

What it does? It creates a new thread in your app. Parameters :

The return value of the CreateThread function is the handle to the created thread. A handle to a thread is like a Window handle (hWnd). It allows you to take control over the thread. If the CreateThread function returns 0, it failed to create the thread.

The next important API call is SetThreadPriority :

Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long

It sets the priority of a specified thread. Parameters :

To get the actual priority of a thread use the GetThreadPriority call.

There are two more interesting threading calls, SuspendThread and ResumeThread :

Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long

Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long

SuspendThread disables a thread and ResumeThread enables a disabled thread. Parameters :

The last call, TerminateThread fully stops a thread. It is important that you stop all threads this way before closing your application because otherwise, it might crash.

Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

Parameters :

 

4. Thank you

Thank you for reading this tutorial. Now you know the most common Win32 multithreading calls, you can create, stop, enable, disable threads and you can change the priority of threads. Did you learn something? If yes, please vote for me. And excuse me for my bad english because Iīm german.

Philipp Weidmann