Example progam
Previous  Next

This is a skeleton program showing how a threading application might be temporarily modified to allow some measure of debugging under the IDE.

Thread is a Thread object.

Sub Globals
      Optimising = false ' running in IDE, set true for compiling 
      Dim LockFlags(0) ' used to synchronise some resources
      LockFlags() = StrSplit("false,false,false,false",",")       
End Sub

Sub App_Start
      ' ... initialise stuff
      Form1.Show
      ' start the thread
      Thread.New1(B4PObject(1))
      If Optimising Then
            Thread.Start("ThreadCode")
      Else
            ThreadCode 
      End If
      ' .. more stuff
End Sub


Sub NeedsResource
      ' ... prepare
      If Optimising Then
            GotIt = Thread.RunLocked1("GetLockFlags",0)
      Else
            GotIt = GetLockFlags(0)
      End If
      If GotIt
            ' ... play with resource
      End If
      If Optimising Then
            Thread.RunLocked1("FreeLockFlags",0)            
      Else
            FreeLockFlags(0)
      End If
      ' ... finish
End Sub

Sub GetLockFlags(n)
      If LockFlags(n) = false Then
            LockFlags(n) = true
            Return true
      Else
            Return false
      End If
End Sub

Sub FreeLockFlags(n)
      LockFlags(n) = false
End Sub


Sub ThreadCode
      ErrorLabel(ThreadCodeErr1) ' trap errors
      ' ...
      Thread.FireThreadEvent ' events work for testing 
      ' ...
      Thread.FireThreadEvent ' events work for testing
      ' ...
      Return
      ThreadCodeErr1: ' ignore any error
      ' ... aborted or other error to reach here
End Sub

Sub Thread_ThreadEvent
      ' ... do whatever
      ' in the IDE may need DoEvents here, maybe in thread as well
      If Not(Optimising) Then DoEvents
End Sub