PIAsynchStatus Example

 

  

This example places an asynchronous call to GetPoints using the where clause entered into text box Text1 and updates a progress bar using the progress events as the call proceeds.   The status events fired by the PIAsynchStatus object are written to list box List2.  When the call completes the results are written to list box List1 and the number of points returned is written to label lblCount.   Command button Command1 is used to submit the asynchronous call while Command button Command2 can be used to Cancel the call while it is in progress. 

 

The screen shot below shows a run where the call has been cancelled so an incomplete result set is returned and the status reflects the cancel.  Note "tag='*'" is a costly query and is only used here for demonstration purposes.

 

 

Build the PIAsynchStatus example as follows:

1. Create a new project and add a reference to the PISDK library. The progress bar is a control from the Microsoft Windows Common Controls 5.0 (SP2) library which should also be added.

2. Add the visual elements as shown in the first picture.

3. Cut and paste the code below into the project.

Option Explicit
Dim srv As Server
Dim asynch As New PIAsynchStatus
Dim ptlist As PointList
Dim WithEvents asynchevents As PIAsynchStatus
Dim bCancel As Boolean

Private Sub Command1_Click()
On Error GoTo eh
   bCancel = False
   ProgressBar1.Value = 0#
   List1.Clear
   List2.Clear
   Set srv = Servers.DefaultServer
   srv.Open
   Dim strQuery As String
   strQuery = Text1.Text
   If asynchevents Is Nothing Then
      Set asynchevents = asynch
   End If
   Set ptlist = srv.GetPoints(Text1.Text, asynch)
   Timer1.Enabled = True

Exit Sub
eh:
   MsgBox Err.Description
End Sub

Private Sub Command2_Click()
   bCancel = True
End Sub

Private Sub Form_Load()
   Timer1.Enabled = False
   Timer1.Interval = 500
   Text1.Text = "tag='s*'"
   lblCount.Caption = " "
End Sub


Private Sub ShowResults()
      Dim pt As PIPoint
      Dim pierr As PIError
      If asynch.Status = csCancelCompleteWithErrors Or asynch.Status = csCompleteWithErrors Then
         Set pierr = asynch.Result
         List1.AddItem CStr(Hex(pierr.Number)) + pierr.Description
      Else
         For Each pt In ptlist
            List1.AddItem pt.Name
         Next
      End If
      lblCount.Caption = CStr(ptlist.Count)
End Sub

Private Sub Timer1_Timer()
On Error GoTo eh
   If bCancel Then
      On Error Resume Next
      asynch.Cancel
      If Err.Number = cmeASYNCHCOMPLETE Then
         bCancel = False
         List2.AddItem Err.Description
      End If
      On Error GoTo eh
      bCancel = False
   End If
   If asynch.Status = csCancelComplete Or asynch.Status = csComplete _
      Or asynch.Status = csCompleteWithErrors Then
      ShowResults
      Timer1.Enabled = False
   End If
   Exit Sub
eh:
   MsgBox Err.Description
End Sub

Private Sub asynchevents_OnStatusChange(ByVal NewStatus As PISDKCommon.CallStatusConstants)
   List2.AddItem "Status change: " + CStr(NewStatus)
End Sub
Private Sub asynchevents_OnProgressChange(ByVal NewProgress As Double)
   ProgressBar1.Value = NewProgress
   DoEvents
End Sub


Enabling Operational Intelligence