yes you can. I ran into this sort of thing a long time back. What you can do is create a constructor for your class that accepts a System.ComponentModel.ISynchronizeInvoke parameter and pass that the form that is using the class.
for exmple
Code:
''' <summary>
''' Gets or sets the object to invoke to avoid illegal cross thread calls
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property SyncObject() As ISynchronizeInvoke
Get
Return Me._syncObject
End Get
Set(ByVal value As ISynchronizeInvoke)
Me._syncObject = value
End Set
End Property
Code:
''' <summary>
''' Create a new instance of the chatlog class
''' </summary>
''' <param name="iSyncObject">The object that will be invoked (this is used to avoid illegal cross thread
''' calls when updating controls on a form from the newline event)</param>
''' <remarks></remarks>
Public Sub New(ByVal iSyncObject As System.ComponentModel.ISynchronizeInvoke)
Me.SyncObject = iSyncObject
If Not Initialize() Then
MsgBox("Unable to initialize... FFXI must be running")
End If
End Sub
Then you can handle all of the possible cross thread calls in the class like so
Code:
''' <summary>
''' Function called from within the thread to call the newline event. If the SyncObject is set, it will check if InvokeRequired and do so if necessary
''' </summary>
''' <param name="iLineInfo"></param>
''' <remarks></remarks>
Protected Overridable Sub OnNewLine(ByVal iLineInfo As ChatLine)
If iLineInfo.MsgID > _firstLine Then
If Me.SyncObject IsNot Nothing AndAlso Me.SyncObject.InvokeRequired Then
Me.SyncObject.Invoke(New LineAdded(AddressOf OnNewLine), New Object() {iLineInfo})
Else
RaiseEvent NewLine(iLineInfo)
End If
End If
End Sub
And when creating an instance of the class you just pass the form that will be using it so as to prevent any cross thread calls.
Code:
Dim c As New ChatLog(Me)