Important alert: (current site time 4/17/2014 10:48:41 PM EDT)
 

article

Easy multithread in VB (Updated)

Email
Submitted on: 2/14/2002 6:53:07 AM
By: Thomas Raben 
Level: Intermediate
User Rating: By 7 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 44755
(About the author)
 
     Its actualy quite easy to make several threads in VB. All you need is one (1), API call and you're on your way.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				To make serveral threads in VB, you need a single API call and a few constants (this code should be placed in a module)
:

Public Const CTF_COINIT = &H8
Public Const CTF_INSIST = &H1
Public Const CTF_PROCESS_REF = &H4
Public Const CTF_THREAD_REF = &H2

Declare Function SHCreateThread Lib "shlwapi.dll" (ByVal pfnThreadProc As Long, pData As Any, ByVal dwFlags As Long, ByVal pfnCallback As Long) As Long

Next is to make sub for the thread (it will not work for a private).

public sub myNewThread()
dim i as integer

for i = 0 to 99
debug.print "message from a new thread (" & i & ")"
next i
end sub

Last but not least, all you need to do, is invoke the new thread: SHCreateThread AddressOf myNewThread, ByVal 0&, CTF_INSIST, ByVal 0&

That's it.

You have a new thread (make sure to end the sub before you exit the program or vb will crash.)

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/14/2002 8:02:39 AMVisualcode

Ill give it 5 never knew that
(If this comment was disrespectful, please report it.)

 
2/14/2002 8:49:17 AMMike Trivette

Could it really be just that easy? Very simple and easy to understand, but is it safe to use?
(If this comment was disrespectful, please report it.)

 
2/14/2002 1:07:58 PMmichael doering

Hello Thomas,
I think a small example are welcome.

(If this comment was disrespectful, please report it.)

 
2/14/2002 5:58:50 PMNick123

Great!! 5 from me to...Can you handle background processing with this? How to end thread? Works all windows version? Please e-mail me.
(If this comment was disrespectful, please report it.)

 
2/15/2002 6:26:31 AMMaxwolf

This seems almost too easy to be true...
(If this comment was disrespectful, please report it.)

 
2/15/2002 6:31:42 AMMH

Just a note, this API requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 5.0 or later); requires Windows 98 (or Windows 95 with Internet Explorer 5.0 or later).
(If this comment was disrespectful, please report it.)

 
2/15/2002 6:31:43 AMRichard Moss

Hmmm... works fine in the IDE, but if you compile your example (I'm running XP Pro), either nothing happens when the buttons or are clicked, or you are presented with a GPF, so not of much use at all.
(If this comment was disrespectful, please report it.)

 
2/15/2002 11:23:10 AMLitehouse

Heh... I've tried this many times in the past and I can say that this doesn't work all that great in VB6. Sure you may get another thread, but try to do ANYTHING that uses IO, including but not limited to msgbox's or disk writes, your app will crash. Overall it's just not very safe to do this, though it WOULD be extremely handy.
(If this comment was disrespectful, please report it.)

 
2/15/2002 3:13:29 PMJohn

How do i end the sub? VB always crashes for me.
(If this comment was disrespectful, please report it.)

 
2/16/2002 1:09:50 PMXicoCorreia

There is an extraordinary example of how to use multothread in VB at this adress: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1
(If this comment was disrespectful, please report it.)

 
2/16/2002 1:15:44 PMJoseph Hicks

heh... forgot the &txtCodeId=##### on the end of your URL, XicoCorreia...
(If this comment was disrespectful, please report it.)

 
2/19/2002 4:41:24 PMLCensoni

looks to easy to be true...
(If this comment was disrespectful, please report it.)

 
4/15/2002 10:49:37 AMfojtik

This code looks fine, but it is slightly useless. You cannot do from second thread:
1,on errorr xxxx
2,msgbox is not painted in exe file
3,you cannot manipulate with visual objects
(If this comment was disrespectful, please report it.)

 
12/30/2002 1:35:49 AM

Couldn't be easier, couldn't be more useful.

Thank you very much indeed.
(If this comment was disrespectful, please report it.)

 
7/11/2004 7:54:08 PMPradeep Singh

5 points
very nice code
thanx
(If this comment was disrespectful, please report it.)

 
10/11/2004 1:32:18 PMJoost Rongen

Running this code in the VB6 IDE corrupts the IDE and or its settings.
I needed to reinstall it after running this code.
(If this comment was disrespectful, please report it.)

 
10/12/2004 1:08:53 PMMike Douglas

4.5 stars for no sample and questionable use? You guys need to hold on to your votes until you actually use the code!
(If this comment was disrespectful, please report it.)

 
12/27/2005 10:14:12 PMjae

Can you handle background processing with this? How to end thread? Works all windows version? Please e-mail me.

(If this comment was disrespectful, please report it.)

 
2/17/2006 7:19:52 PMMatthew Kernes

I'm not going to even try this code. Sounds like it'd be useful if you were to make a "virus" like program. But it's not much use to me if there's not I/O.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.