Solved

MultiThreaded Apps with VB.

Posted on 1998-11-01
1
233 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

820 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