Download XPert Basic SLL User Manual
Transcript
perform its processing and return quickly. To run a program that loops until some event (e.g., recording start/stop or system shutdown), use StartTask to kick-off a program that monitors for the desired event, sleeping when there’s nothing to do. To run a program at system shutdown, create a subroutine named Stop_Program. All subroutines having this name are run at system shutdown. This subroutine should perform cleanup quickly and return. Note: when defining Start_Program and Stop_Program, don’t declare them as Public, since there could be other subroutines having these names in other .bas files. The following example shows how to start a program at system start, and then signal that program to stop at system shutdown: TimeToStop = 0 Public Function ProcessingLoop(Parm) Sum = 0.0 ' Return 0 to schedule the function for the next minute ProcessingLoop = 0 ' Average A/D channel 1 once per second for the next 10 seconds For i = 1 To 10 Sum = Sum + Ad(1, 1) ' Exit and stop processing if the TimeToStop event is raised ' otherwise delay a second between samples If WaitEvent(1, TimeToStop) = 1 Then ProcessingLoop = -1 Exit Function End If Next i End Function Sub START_PROGRAM StatusMsg "Start Program" ResetEvent TimeToStop REM Run the main processing loop every minute on the minute StartTask "ProcessingLoop", 0, TimeSerial(0, 0, 0), TimeSerial(0, 1, 0) End Sub Sub STOP_PROGRAM StatusMsg "Stop Program" SetEvent TimeToStop StopTask "ProcessingLoop" End Sub Multi-threading Xpert Basic programs often run in independent program threads (e.g., programs started with StartTask, scheduled subroutines, and setup blocks). There are several concerns related to multithreaded operation to address: Resource Contention The first concern is that subroutines executing in different threads may require access to the same variable, allowing the possibility of data corruption. When a subroutine uses only local variables, there is no concern of data corruption. However, when a subroutine uses global resources (e.g., a global variable or com port), it is important to synchronize thread access to the resource so that only a single thread will use it at any given time. 38