Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MultiThreaded Apps with VB.

Posted on 1998-11-01
1
Medium Priority
?
241 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

971 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