Running Events at the same time

ok... I have an array of winsock controlls. the winsock controlls are called wsktoclient. when a wsktoclient data arival event is risen(index one we'll say) it takes a long to time to exicute... it could take hours(it sends files in this event). when another one is risen(index two) the program exicutes the index two event and does not work on index one untill it is finished with the index two event. how do i keep both events exicuting simultaniously?
hessAsked:
Who is Participating?
 
AnswerTheManConnect With a Mentor Commented:
your problem is very well described in this qoutation from MSDN :
<<""
Caution:   Any time you temporarily yield the processor within an event procedure, make sure theprocedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results.
">>

now, to your question : "how do i keep both events exicuting simultaniously?" -
there is only one answer - *THREADS* .

i can assure you that no trick will make your current concept work unless you'll have only 1-2 clients....

i recommend you to create ActiveX EXE object that will be defined as 'Thread Per Object', and that will contain that winsock control.
you main app cam instanciate as many objects like that as you want (depending on machine\server resources), and each will run in his own thread. the Operating system will take care to execute the events simulantly.

if you don't know how to create ActiveX EXE - post a question.....good luck


 
0
 
corvanderlindenCommented:
If you are using NT or Win9x you could put your events in seperate threads. Or you could make a single use out-of-process ActiveX exe that executes the event.

Hope this helps,
Cor
0
 
hessAuthor Commented:
That is what i wnat to know how to do... i should have stated it better. how do i put hte events in seperate threads
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
hessAuthor Commented:
or where can i learn to use more threads... that is currently above me
0
 
corvanderlindenCommented:
Here is a sample from Bruce McKinneys book. Hope it helps to get you started

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
'
0
 
hessAuthor Commented:
I have a max of 16 clients(it can be expanded) and i used timers to solve my problem. now i don't get traped in one event for a long time... things all work now. thankyou... i'll deal with threads later
0
 
AnswerTheManCommented:
if the data traffic is heavy - your way won't do the job even 80%. data will be lost.
Good Luck anyway.
0
 
hessAuthor Commented:
why... i can send out a max of 15 files... and the timers turn themselves on and off..15 at a time is the max that i need to send
0
 
hessAuthor Commented:
why... i can send out a max of 15 files... and the timers turn themselves on and off..15 at a time is the max that i need to send
0
 
AnswerTheManCommented:
that's not multithreading. that's ONE thread doing all that...
you may get away with it in this one - but it ain't a serius programing.
when you'll come to REAL stuff this won't be enough.
0
All Courses

From novice to tech pro — start learning today.