Solved

MultiThreaded Apps with VB.

Posted on 1998-11-01
1
229 Views
Last Modified: 2010-04-30
Is it possible to create Multithreaded applications with VB without using any activex controls? If possible, then what is the procedure?
0
Comment
Question by:pagladasu
1 Comment
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1442793
Yes, it is possible, although it is a hassle.  Here's an idea (NOT the exact code).

Include the following in the General section of a module:
Public Declare Function CreateThread Lib "kernel32" Alias "CreateThread" (lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal dwStackSize As Long, lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Public Declare Function SetThreadPriority Lib "kernel32" Alias "SetThreadPriority" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Public Declare Function ResumeThread Lib "kernel32" Alias "ResumeThread" (ByVal hThread As Long) As Long
Public Declare Function TerminateThread Lib "kernel32" Alias "TerminateThread" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

Write a sub that you want to execute in a different thread:
    Sub SeparateThreadSub
         ' Whatever code is here
    End Sub

In your main program:

1. Call CreateThread passing Address Of SeparateThreadSub as the address parameter and include CreateSuspended flag.  This will return the thread handle or 0 if an error occurs.
2. Call SetThreadPriority passing this thread's handle and the priority you need.  If you don't need to set the priority, you can exclude the CreateSuspended flag in the first step and omit this one and step 3.  That way, a default priority will be used.
3. Call ResumeThread function passing this thread's handle as the parameter.

The thread will start executing your sub while you can do whatever else work you need.  After it is done with your sub, it will exit automatically.  If you need to stop this thread before it's done, use the TerminateThread function.

If you need more details, there was an article on how to do this in the VBPJ magazine, I believe this year or maybe last year.  You can go to their web site
(http://www.windx.com/free/magazines/VBPJ/vbpj.asp) and try searching the VBPJ archives.  You'll need the Premier level access, which is not free.

Note that it's really quite a pain to create threads with VB. The easiest language to write multi-threaded applications is Java.  If multi-threading is essential, you may want to look into it.

Hope this gives you an idea,
-Aleks.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question