Solved

How do i create a seperate thread

Posted on 2000-03-11
21
206 Views
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?
0
Comment
Question by:hess
  • 7
  • 6
  • 5
  • +1
21 Comments
 
LVL 2

Expert Comment

by:johnny6
ID: 2608158
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
 
LVL 2

Expert Comment

by:johnny6
ID: 2608164
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
 
LVL 2

Expert Comment

by:johnny6
ID: 2608178
hess:
    Here is an excellent article on multithreading by Dan Appleman:

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

John

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2608240
Why not just use an ActiveX.exe component? Intraprogram communication is assured through your interface and it can run asynchronious.
0
 
LVL 2

Expert Comment

by:johnny6
ID: 2608379
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
 

Author Comment

by:hess
ID: 2610091
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
 
LVL 10

Accepted Solution

by:
caraf_g earned 500 total points
ID: 2610319
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623687
Well, hess, did it help?
0
 

Author Comment

by:hess
ID: 2625878
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2633365
But, hess, in your question you mention the word thread at least three times. What was I to think you were asking for?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:hess
ID: 2634936
it is what i asked for but since then i have changed my mind.
0
 

Author Comment

by:hess
ID: 2634938
how would programs turn out if people never changed their mind... it seemed as though you're mad at me?
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2634993
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2635134
I'm not mad, I'm an aeroplane

Weeeeeeeeeeee
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2644088
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2652477
wsh2, johnny6, what do you think?
0
 
LVL 2

Expert Comment

by:johnny6
ID: 2653369
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2654360
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2656344
0
 
LVL 2

Expert Comment

by:johnny6
ID: 2657610
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
 

Author Comment

by:hess
ID: 2657727
"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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now