We help IT Professionals succeed at work.

How do i create a seperate thread

Last Modified: 2013-11-13
   I need to know how to create up to 16 seperate Threads that will be used to send files through winsock. Once these threads are in exicution will they have acess to my global variables?

    So How can i create up to 16 threads that will run simultaniously and be able to accsess the winsock controls on another form?
Watch Question

    Check out the folowing code on how to create threads in VB: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=4984


     Here is some additional code for threads:

Put following in a .bas module

Option Explicit

Declare Function CreateThread Lib "KERNEL32" ( _
ByRef lpThreadAttributes As Any, _
ByVal dwStackSize As Long, _
ByVal lpStartAddress As Long, _
ByRef lpParameter As Any, _
ByVal dwCreationFlags As Long, _
ByRef lpThreadId As Long) As Long

Declare Sub ExitThread Lib "KERNEL32" ( _
ByVal dwExitCode As Long)

Declare Sub CloseHandle Lib "KERNEL32" ( _
ByVal h As Long)

Declare Function GetExitCodeThread Lib "KERNEL32" ( _
ByVal hThread As Long, _
ByRef lpExitCode As Long) As Long

Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)
Declare Function GetTickCount Lib "KERNEL32" () As Long

Const STILL_ACTIVE = 259
Const pNull As Long = 0

Private fRunning As Boolean
Private cCalc As Long
Private cAPI As Long
Private datBasic As Date
Private hThread As Long
Private idThread As Long

Sub StartThread(ByVal i As Long)
' Signal ThreadProc that thread is starting
fRunning = True
' Create new thread
hThread = CreateThread(ByVal pNull, 0, AddressOf ThreadProc, _
ByVal i, 0, idThread)
If hThread = 0 Then MsgBox "Can't start thread"
End Sub

Function StopThread() As Long
' Signal thread to stop
' Next statement signals ThreadProc to ExitThread
fRunning = False

' Make sure thread is dead before returning exit code
Call GetExitCodeThread(hThread, StopThread)
Loop While StopThread = STILL_ACTIVE

CloseHandle hThread
hThread = 0
End Function

Function ThreadRunning() As Boolean
ThreadRunning = fRunning
End Function

Function CalcCount() As Long
CalcCount = cCalc
End Function

Function APICount() As Long
APICount = cAPI
End Function

Function BasicTime() As Date
BasicTime = datBasic
End Function

Sub ThreadProc(ByVal i As Long)
' Use parameter
cCalc = i
Do While fRunning
' Calculate something
cCalc = cCalc + 1
' Use an API call
cAPI = GetTickCount
' Use a Basic function
datBasic = Now
' Switch immediately to another thread
Sleep 1
' Return a value
ExitThread cCalc
End Sub


    Here is an excellent article on multithreading by Dan Appleman:



Why not just use an ActiveX.exe component? Intraprogram communication is assured through your interface and it can run asynchronious.

    You can find some sample code for Creating an ActiveX EXE in the following previously asked question:



Is Multi-Threading Right For Me?

Here is the reason i was looking to Multi-Thread:

    I have a filesharing client that I have developed that you can look at at http://vandel405.hypermart.net .  Users complain that the transfers timeout sometimes (rarely). I use an array of upto 16 winsock controls and timers for sending files.  The timer gets fired every time a sendcomplete event happens for the associated Winsock control.  It appears that sometimes the sendcomplete event might not be fireing. is this posible?  If i arbitarily fired the timer again to send off more data would the data that should have been fireing the sendcomplete event the first time  be lost or would all the data still arive at it's destination. Are These legitimat time outs?

---- I'm posting this under a new question since it is far out of the scope of my previous question. Your help on multi threading was great but i think it might be more than i need. If you disagree let me know.

Here is the url to the other question
This one is on us!
(Get your first solution completely free - no credit card required)

Well, hess, did it help?


well I haven't tried it because i've desided taht iw as just hoping to mult thread rather than clean my event driven program. i think my program just needs some cleaning rather than multi threading

But, hess, in your question you mention the word thread at least three times. What was I to think you were asking for?


it is what i asked for but since then i have changed my mind.


how would programs turn out if people never changed their mind... it seemed as though you're mad at me?

Nah.. he's not mad.. we love spending our time researching USELESS pieces of information and are only too happy to do the thinking others should have done before we started.

As Caraf G (or anyone else here)inadvertently got you going in the right direction, at least a modicum of acknowledgement is in order. Remember, it wasn't he/she that wrote the question.. <wink>.

They say, a camel is a horse designed by indecisive people.. as such, don't be surprised if you get DESERTed along the way.. <lol> and a <smile>.

I'm not mad, I'm an aeroplane


Anyway, I believe I answered your original question. It's your prerogative to change your mind. But that doesn't change the fact that I did answer your question. Plus... I think it was a pretty good answer <g>

wsh2, johnny6, what do you think?

      Considering how mush work each of us put into answering this question I think that hess should award the points to one of us.


What do I think?... I think Hess is gonna be a real lame @$$.. and stiff ya on the points (Uh... hmmm.. do I get any points for providing this CORRECT answer?... <LOL>). Hope I'm wrong fellows, we'll see.. <smile>.

     Since I was the first person to post a comment to your threads question, I am answering this question.

If you feel that I have not done a satisfactory job of answering your question about threads, then you are free to disagree and reject my answer.  

You should be aware that I feel that all three of the people who attempted to answer your question provided you with a correct answer.

Therefore, you should either accept my answer and award me the points or reject my answer and award no one the points.  In the later case you will be reported to the Experts-Exchange staff and you may risk losing your Experts-Exchange account.

One of the reasons I would like to see you accept an answer from one of us is because of the fact that you asked a very good question.  Many other people are likely to purchase and view this question after it becomes a Previously Answered Question.

I hope we can settle this dispute in a friendly manner.



"In the later case you will be reported to the Experts-Exchange staff and you may risk losing your Experts-Exchange account."
--->Even if i did lose my account i could just get one with a new email---

You're a total ass. I dont like being threatened. Thats not expertmanly like conduct. maybe I should report you.

I like carls answer more

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.