Code-Beispiel
Arbeiten mit Threads
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | Mao | 25.05.2012 |
Manchmal ist es nützlich, wenn man mehrere Dinge (scheinbar) gleichzeitig ablaufen lässt. Mit Threads ist dies möglich:
(alte Schreibweise)
Const anzahl_treads=2
Dim Shared thread(anzahl_treads) As Integer
Declare Sub thread1 (ByVal num As Integer) ' Thread Sub 1
Declare Sub thread2 (ByVal num As Integer) ' Thread Sub 2
Declare Sub warten (duration as double)
Locate 1,1 : Print "Starten von Threads!"
thread(1)=threadcreate(@thread1, 1) ' Starte thread1
thread(2)=threadcreate(@thread2, 2) ' Starte thread2
Threadwait(thread(1)) ' warten auf thread1
Threadwait(thread(2)) ' warten auf thread2
Locate 1,1: Print "Threads beendet! "
Sleep
Sub thread1 (ByVal num As Integer)
Dim sLine As String
Dim aLine As String
Dim bLine As String
Dim i As Integer
sLine = "Test Text aus Thread 1"
For i = 0 To len(sLine)
aLine = mid(sLine,i,1)
bLine = bLine + aLine
Locate 3,1: Print bLine
warten 0.4
Next i
End Sub
Sub thread2 (ByVal num As Integer)
Dim sLine As String
Dim aLine As String
Dim bLine As String
Dim i as integer
sLine = "Test Text aus Thread 2"
For i = 0 To len(sLine)
aLine = mid(sLine,i,1)
bLine = bLine + aLine
Locate 5,1: Print bLine
warten 0.6
Next i
End Sub
Sub warten (duration As double)
Dim StartTimer As double
StartTimer = Timer
While Timer - StartTimer < Duration
Wend
End Sub
Autor: Benjamin Kunz
Quelle: fb@rchiv
ab FB 0.18 :
Dim Thread(1 To 2) As Any Ptr
Declare Sub Prog1 (param As Any Ptr) ' Thread Sub 1
Declare Sub Prog2 (param As Any Ptr) ' Thread Sub 2
Declare Sub warten (duration As Single)
Dim As Single warte1=0.4 , warte2=0.5
Locate 1,1 : Print "Starten von Threads!"
' Starte Thread1 mit Parameter
Thread(1)=ThreadCreate(@Prog1,@warte1)
' Starte Thread2 mit Parameter
Thread(2)=ThreadCreate(@Prog2,@warte2)
ThreadWait(Thread(1)) ' warten auf Thread1
ThreadWait(Thread(2)) ' warten auf Thread2
Locate 1,1: Print "Threads beendet! ";
Sleep
Sub Prog1 (param As Any Ptr)
Dim i As Integer
Dim As String sLine = "1. Test Text aus Thread 1"
For i = 1 To Len(sLine)
Locate 3,i: Print Mid(sLine,i,1)
warten *Cast (Single Ptr,param)
Next i
End Sub
Sub Prog2 (param As Any Ptr)
Dim i As Integer
Dim As String sLine= "2. Test Text aus Thread 2"
For i = 1 To Len(sLine)
Locate 5,i: Print Mid(sLine,i,1)
warten *Cast (Single Ptr,param)
Next i
End Sub
Sub warten (duration As Single)
Dim As Single StartTimer = Timer
While Timer - StartTimer < Duration
Wend
End Sub
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|