Multithreading in VB!!! (updated)
Finally it is there!The first multithreading example on PSC. About half a year ago, I looked on PSC for some multithreading stuff and found nothing. Then when I had improved my skills, I finally made my own multithreading code.Now have fun with this and vote for me!!!Updates:-Fixed the bug that the program didn´t exit properly-Added code in the multithreading class that prevents a thread from being created twice-Fixed the bug in the PropertyLet procedure Priority (Thanks to Gil for letting me know). It worked with VB6 but it didn´t compile with VB5 and now it works
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
<html> <head> <meta http-equiv="Content-Language" content="de"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>Multithreading Tutorial</title> </head> <body> <p align="center"><font size="6" color="#0000FF"><u>Multithreading</u></font></p> <p align="center">Hi, welcome to this little tuturial on Win32 multithreading in Visual Basic.</p> <p align="center">For more mutithreading examples download the .zip file which includes a full sample project and my clsThreading.cls which makes multithreading much easier.</p> <p align="center">This tutorial is also included in the .zip file so you don´t have to read it here.</p> <p align="center"><b>1. What is multitasking?</b></p> <p align="center">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.</p> <p align="center">You can switch between these tasks using the buttons in the taskbar.Well, they do not really run at the same time, because only <i>one</i> 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.</p> <p align="center">This ability of Windows to handle various tasks at once is called <b><i>multitasking</i></b>. </p> <p align="center"> </p> <p align="center"><b>2. What is multithreading?</b> </p> <p align="center">But Windows can do even more. </p> <p align="center">Not only various tasks can run at once, but one task can create multiple <i>threads</i>, 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. </p> <p align="center">The ability of Windows to allow one app to handle multiple threads is called <b><i>multithreading</i></b>. </p> <p align="center"> </p> <p align="center"><b>3. How can I implement multithreading in my VB program?</b> </p> <p align="center">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) : </p> <p align="center">The first and most important is the <b><font color="#0000FF">CreateThread</font></b> call : </p> <p align="center"><font face="Fixedsys">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</font> </p> <p align="center">What it does? It creates a new thread in your app. Parameters :</p> <ul> <li> <p align="left"><font face="Fixedsys">ByVal lpThreadAttributes As Any</font>: The security attributes for the threads. The normal type is <font face="Fixedsys">SECURITY_ATTRIBUTES</font>, but you don´t need this parameter so use <font face="Fixedsys">ByVal 0& </font>as value.</li> <li> <p align="left"><font face="Fixedsys">ByVal dwStackSize As Long</font>: Tells Windows how much stack memory the thread should have. We don´t need it, use <font face="Fixedsys">ByVal 0& </font>for this parameter.</li> <li> <p align="left"><font face="Fixedsys">ByVal lpStartAddress As Long</font>: This is the most important parameter. It tells Windows which function the thread has to execute. To use this parameter, we need the <font face="Fixedsys">AddressOf </font>operator which can be used with public functions in public modules only. So place your threaded function in a module and for the lpStartAddress parameter use <font face="Fixedsys">AddressOf YourFunction</font>.</li> <li> <p align="left"><font face="Fixedsys">ByVal dwCreationFlags As Long</font>: The creation flags for the thread. To use this parameter, search the <font face="Fixedsys">CREATE_* </font>constants in the API Viewer (they are also in clsThreading.cls). One interesting flag is the <font face="Fixedsys">CREATE_SUSPENDED</font> flag, which allows you to create the thread disabled (not running). If you don´t need this parameter, use <font face="Fixedsys">ByVal 0&</font>.</li> <li> <p align="left"><font face="Fixedsys">lpThreadID As Long</font>: This is a <font face="Fixedsys">ByRef </font>parameter which represents the ID of the created thread.</li> </ul> <p align="center">The return value of the <font face="Fixedsys">CreateThread </font>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 <font face="Fixedsys">CreateThread </font>function returns 0, it failed to create the thread.</p> <p align="center">The next important API call is <b><font color="#0000FF">SetThreadPriority</font></b> :</p> <p align="center"><font face="Fixedsys">Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long</font></p> <p align="center">It sets the priority of a specified thread. Parameters :</p> <ul> <li> <p align="left"><font face="Fixedsys">ByVal hThread As Long</font>: The handle to the thread. You can use for example the return value of the <font face="Fixedsys">CreateThread</font> call.</li> <li> <p align="left"><font face="Fixedsys">ByVal nPriority As Long</font>: The new priority of the specified thread. The thread priority has five major values: <font face="Fixedsys">THREAD_PRIORITY_LOWEST</font>, <font face="Fixedsys">THREAD_PRIORITY_BELOW_NORMAL</font>, <font face="Fixedsys">THREAD_PRIORITY_NORMAL</font>, <font face="Fixedsys">THREAD_PRIORITY_ABOVE_NORMAL</font> and <font face="Fixedsys">THREAD_PRIORITY_HIGHEST</font> which should be self-explaining. The constants for these values can be found in the API viewer (and in clsThreading.cls).</li> </ul> <p align="center">To get the actual priority of a thread use the <font color="#0000FF"><b>GetThreadPriority </b></font>call.</p> <p align="center">There are two more interesting threading calls, <b><font color="#0000FF">SuspendThread</font></b> and <font color="#0000FF"><b>ResumeThread</b></font> :</p> <p align="center"><font face="Fixedsys">Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long</font></p> <p align="center"><font face="Fixedsys">Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long</font></p> <p align="center"><font color="#0000FF"><b>SuspendThread</b></font> disables a thread and <font color="#0000FF"><b>ResumeThread</b></font> enables a disabled thread. Parameters :</p> <ul> <li> <p align="left"><font face="Fixedsys">ByVal hThread As Long</font>: The handle to the thread we want to disable/enable.</li> </ul> <p align="center">The last call, <font color="#0000FF"><b>TerminateThread</b></font> fully stops a thread. It is important that you stop all threads this way before closing your application because otherwise, it might crash.</p> <p align="center"><font face="Fixedsys">Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long</font></p> <p align="center">Parameters :</p> <ul> <li> <p align="left"><font face="Fixedsys">ByVal hThread As Long</font>: We know it: The handle to the thread we want to terminate.</li> <li> <p align="left"><font face="Fixedsys">ByVal dwExitCode As Long</font>: An exit code (not needed). Use <font face="Fixedsys">ByVal 0&</font> for this parameter.</li> </ul> <p align="center"> </p> <p align="center"><b>4. Thank you</b></p> <p align="center">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.</p> <p align="center"><b>Philipp Weidmann</b></p> </body> </html>