VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "LaunchP4Connect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

' This class builds on the example from www.powervb.com

Private Controller As ThreadControl
Private Cookies As Collection

Public Sub LaunchThreads()
Dim CLSID As CLSID
Dim Cookie As Long
    'Create threads to run p4connects. The returned
    'data in this scenario is retrieved using the Cookie
    'values returned by CreateWorkerThread.
    CLSID = CLSIDFromProgID("P4ComAddin.P4ConnectThread")
    Controller.CreateWorkerThread CLSID, 3000, True, Cookie
    Cookies.Add Cookie, CStr(Cookie)
End Sub

Public Sub FinishThreads()
    Controller.StopWorkerThreads
End Sub

Public Sub CleanCompletedThreads()
    Controller.CleanCompletedThreads
End Sub

Public Sub ShowResults()
Dim Iter As Variant
Dim OutputVar As Variant
Dim ExitCode As Long
Dim hr As Long
    'For Each Iter In Cookies
    '    If Controller.GetWorkerOutput(Iter, hr, ExitCode, OutputVar) Then
    '        MsgBox Hex$(hr) & ", " & CStr(ExitCode) & ": " & OutputVar
    '        Cookies.Remove CStr(Iter)
    '    End If
    'Next
End Sub

Public Function ThreadFinished(Result As String) As Boolean
Dim Iter As Variant
Dim OutputVar As Variant
Dim ExitCode As Long
Dim hr As Long
    For Each Iter In Cookies
        If Controller.GetWorkerOutput(Iter, hr, ExitCode, OutputVar) Then
            Result = OutputVar
            ThreadFinished = True
            Cookies.Remove CStr(Iter)
        End If
    Next
End Function

Private Sub Class_Initialize()
    Set Cookies = New Collection
    Set Controller = New ThreadControl
End Sub

Private Sub Class_Terminate()
    Controller.StopWorkerThreads
    Set Controller = Nothing
End Sub

