' ' The purpose of this program is to demonstrate the technique of invoking ' multiple instances of the same task. While this may not be generally ' useful, it may be a perfect solution for special situations. ' ' In this code, an array of semaphores is used to allow each instance of ' the task to identify itself. When an instance begins running, it ' indexes the array attempting to get a semaphore on an element of the ' array. The index of the array at which it succeeds in getting a semaphore ' is taken to be the ID of that instance of the task. Depending on the ' particular application, it may not be necessary to have an ID associated ' with each instance. In this test code, the ID serves to differentiate ' the output of the several instances. ' This semaphore is used to avoid overlapping output among the instances. Dim ioSem as Boolean ' This array of semaphores is used to allow the instances to self-identify. Dim sem(1 to 3) as Boolean ' task stacks Const tsSize as Byte = 65 ' this seem to be about the minimum to be able to run Dim ts1(1 to tsSize) As Byte Dim ts2(1 to tsSize) As Byte Dim ts3(1 to tsSize) As Byte Sub Main() ioSem = false sem(1) = false sem(2) = false sem(3) = false ' invoke several instances of the same task CallTask "MyTask", ts1 CallTask "MyTask", ts2 CallTask "MyTask", ts3 Do Loop End Sub Sub MyTask() Dim i as Byte Dim myID as Byte ' find out which invocation this is by getting the next available semaphore myID = 0 For i = 1 to 3 If (Semaphore(sem(i))) Then myID = i Exit For End If Next For i = 1 to 20 ' get the I/O semaphore to avoid overlapping output Do While Not Semaphore(ioSem) Call Sleep(0) Loop ' output a string indicating what's happening now Debug.Print "T-"; Debug.Print CStr(myId); Debug.Print ":"; Debug.print CStr(i) ioSem = false Call Delay(0.5) ' determine if it is time to exit yet If (i = myID * 3) Then Exit For End If Next ' get the I/O semaphore to avoid overlapping output Do While Not Semaphore(ioSem) Call Sleep(0) Loop ' display a termination message Debug.Print "T-"; Debug.Print CStr(myId); Debug.Print " done" ioSem = false End Sub