Solved

MultiThreaded Apps with VB.

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now