Link to home
Create AccountLog in
Avatar of jack niekerk
jack niekerkFlag for Netherlands

asked on

2 OR MORE TIMER ROUTINES IN VB6

I read 4 comport for data
Now use 1 timer sub with 1 second interval
in sub I handle first com1 data will take 0.5 sec.
then in same sub com2 , also 0.5 sec  and same for com3 and com4
so every handling for 4 comports takes 2 seconds in timer sub
can I speed up  with disable timer while in timer loop
or make 4 separate timer subs wich each handle 1 com port?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

If you are trying to see how long code takes to run, don't depend on the Timer since it's not very accurate. See my article on timing code.
Avatar of jack niekerk

ASKER

Ok, this sounds very logical,  but its not a issue of measuring the time it takes
This is my code,  it handles top down the (if any) databuffers in de comports
I was wandering if ity would be possible to handle the 4 port at the same time
meaning that if i make 4 subs all 4 ports will be handle at the same time
unless you mean that windows will run 1 sub at the time not multi tasking
if that would be the case then there is nothing to gain in speed
Thanks
Private Sub tmrscanport_Timer()
'==============================
 If Len(Trim(DataIn1$)) >= 38 Then
    For DataLen = 1 To Fix(Len(DataIn1$) / 38) Step 38
    If Asc(Mid$(DataIn1$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn1$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn1$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn1$, DataLen + 24, 1)) <= lbltag.UBound Then
       tagNumber = Asc(Mid$(DataIn1$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn1$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn1$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn1$, DataLen + 24, 1))
      lblpoort(tagNumber).Caption = "com1"
      lbltag(tagNumber).Caption = Str$(tagNumber)
     End If
    Next
End If


 If Len(Trim(DataIn2$)) >= 38 Then
    For DataLen = 1 To Fix(Len(DataIn2$) / 38) Step 38
    If Asc(Mid$(DataIn2$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn2$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn2$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn2$, DataLen + 24, 1)) <= lbltag.UBound Then
       tagNumber = Asc(Mid$(DataIn2$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn2$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn2$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn2$, DataLen + 24, 1))
       lblpoort(tagNumber).Caption = "com2"
       lbltag(tagNumber).Caption = Str$(tagNumber)
    End If
      Next
End If


 If Len(Trim(DataIn3$)) >= 38 Then
    For DataLen = 1 To Fix(Len(DataIn3$) / 38) Step 38
   If Asc(Mid$(DataIn3$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn3$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn3$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn3$, DataLen + 24, 1)) <= lbltag.UBound Then
    tagNumber = Asc(Mid$(DataIn3$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn3$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn3$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn3$, DataLen + 24, 1))
     lblpoort(tagNumber).Caption = "com3"
     lbltag(tagNumber).Caption = Str$(tagNumber)
   End If
    Next
End If


 If Len(Trim(DataIn4$)) >= 38 Then
    For DataLen = 1 To Fix(Len(DataIn4$) / 38) Step 38
     If Asc(Mid$(DataIn4$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn4$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn4$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn4$, DataLen + 24, 1)) <= lbltag.UBound Then
       tagNumber = Asc(Mid$(DataIn4$, DataLen + 21, 1)) * 2 ^ 24 + Asc(Mid$(DataIn4$, DataLen + 22, 1)) * 2 ^ 16 + Asc(Mid$(DataIn4$, DataLen + 23, 1)) * 2 ^ 8 + Asc(Mid$(DataIn4$, DataLen + 24, 1))
       lblpoort(tagNumber).Caption = "com4"
       lbltag(tagNumber).Caption = Str$(tagNumber)
    End If
    Next
End If
   
End Sub
As a genera statement, VB6 does nothing at the same time.

However by looking at your code it doesn't look like you are defining your variables. If that's the case then VB6 will treat them as Variants which are very slow. So do something like

Dim DataLen As Long

You also might be better off doing everything in one timer. You could easily try that just to see what happens.
Try this Multithreading approach using pure VB6.
(Download Multithrea985736252002.zip).
- Please run the demo as a compiled exe only.
- Compile it as Activex.exe. Set the threading options to "Thread Per Object".
You can create as many threads as you like using VB6, but VB6 is limited to the single threaded apartment model, so only one thread executes at any one time.
Dim DataLen As Long  is in
 Public DataLen As Long

will start a test with demo from eemit
If you need different tests at different intervals, it would be logical to have four separate Timer controls.
Hi eemit, wich one should I use?
See screen copy please
thanks
this was not uploaded , so again
MULTI.bmp
Hi BIAPRO,
I posted a link in my Post-ID: 38589663.
Click on "Download article" to download the zip file: Multithrea985736252002.zip
thanks
ASKER CERTIFIED SOLUTION
Avatar of eemit
eemit
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account