Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

How do i create a seperate thread

   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?
0
hess
Asked:
hess
  • 7
  • 6
  • 5
  • +1
1 Solution
 
johnny6Commented:
hess:
    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


John
0
 
johnny6Commented:
hess:
     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
Do
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
Loop
' Return a value
ExitThread cCalc
End Sub
'
APP



John
0
 
johnny6Commented:
hess:
    Here is an excellent article on multithreading by Dan Appleman:

http://www.desaware.com/articles/threadingL3.htm

John

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
wsh2Commented:
Why not just use an ActiveX.exe component? Intraprogram communication is assured through your interface and it can run asynchronious.
0
 
johnny6Commented:
hess:
    You can find some sample code for Creating an ActiveX EXE in the following previously asked question:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10232357 

John

0
 
hessAuthor Commented:
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
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10309663 
0
 
caraf_gCommented:
Threading without rocket science.

http://homepage.eircom.net/~carafa/TEMP/AWXB.zip

Explanation / order of compilation
AWXB02MSG.exe - (ActiveX.exe, one thread for all objects) Messaging application that will take care of sending messages between spawned threads and the main application.
AWXB01Thread.dll - The code that will be run in each separate thread
AWXB04Starter.exe - (ActiveX.exe, one thread per object) Application that will take care of creating each new thread.
AWXB03Test.exe - Standard exe. Main application / test bed.

Hope this helps.
0
 
caraf_gCommented:
Well, hess, did it help?
0
 
hessAuthor Commented:
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
0
 
caraf_gCommented:
But, hess, in your question you mention the word thread at least three times. What was I to think you were asking for?
0
 
hessAuthor Commented:
it is what i asked for but since then i have changed my mind.
0
 
hessAuthor Commented:
how would programs turn out if people never changed their mind... it seemed as though you're mad at me?
0
 
wsh2Commented:
Hess:
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>.

0
 
caraf_gCommented:
I'm not mad, I'm an aeroplane

Weeeeeeeeeeee
0
 
caraf_gCommented:
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>
0
 
caraf_gCommented:
wsh2, johnny6, what do you think?
0
 
johnny6Commented:
caraf_g:
      Considering how mush work each of us put into answering this question I think that hess should award the points to one of us.

John
0
 
wsh2Commented:
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>.
0
 
johnny6Commented:
hess:
     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.


John
0
 
hessAuthor Commented:
"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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now