Solved

MultiThreaded Apps with VB.

Posted on 1998-11-01
1
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

710 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