我们有一个Windows Forms应用程序实例化一个dll,并在该dll中实例化类。
修改
所以这个过程是这样的:
FORM> alpsClient(DLL)>类
我想从类中更新FORM上的backgroundWorker_ProgressChanged
事件。
结束编辑
在类对象中完成工作可能需要一段时间。
我希望将某些类型的进度从课程传回给调用表单。
我想我需要使用事件。
但是我应该如何引用在后台工作者中启动该过程的表单。
以下是实例化dll对象的代码:
Private Sub alpsManual_bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles alpsManual_bgw.DoWork
Try
Dim arg As String = CStr(e.Argument)
objDoc = New alpsClient
'* Set variables and start
' CUT ...
If fromArchive Then
'********************************************
'* Manual Settings *
'********************************************
' SET PROPERTIES
... CUT for brevity
'********************************************
End If
objDoc.docMap()
'* Wait until isComplete before starting next
Do
System.Threading.Thread.Sleep(1000)
Loop Until objDoc.isComplete
Catch ex As Exception
以下是将在alpsClient.docMap
:
Dim objControl As Object = Nothing
Select Case docRef
Case "YL0PF" 'Arrears Advice
objControl = New YL0PF
Case "YL2PF" 'Arrears with Additional Interest
objControl = New YL2PF
... etc
然后:
objControl.dbConn = masterDB
objControl.docRef = docRef
objControl.table2Format = table2Format
objControl.newAddressFormat = newAddressFormat
objControl.dteinp = runDate
' Start
docMap = ""
If Not objControl.Controller() Then docMap = "Errors"
并且它在objControl
引用的类中,我想将进度消息发送回调用表单...可能使用事件。
或者这是一个非常糟糕的主意?
答案 0 :(得分:3)
您可以确保所有控件类都从公共基类继承,例如:
Public MustInherit Class ControlBase
Protected Sub New()
End Sub
Public Event ProgressEvent As EventHandler<Of ProgressEventArgs>
Protected Sub RaiseProgressEvent(current As Integer, maximum as Integer)
RaiseEvent ProgressEvent(Me, New ProgressEventArgs(current, maximum)
End Sub
End Class
Public Class ProgressEventArgs : Inherits EventArgs
Private _current as Integer
Private _maximum as Integer
Public Sub New(current As Integer, maximum as Integer)
_current = current
_maximum = maximum
End Sub
Public ReadOnly Property Current() As Integer
Get
Return _current
End Get
End Property
Public ReadOnly Property Maximum() As Integer
Get
Return _maximum
End Get
End Property
End Class
然后你的每个YL0PF,YL2PF等等都继承自这个类并调用RaiseProgressEvent。
当你教导课程时,做这样的事情......
Dim objControl As ControlBase = Nothing
Select Case docRef
Case "YL0PF" 'Arrears Advice
objControl = New YL0PF
Case "YL2PF" 'Arrears with Additional Interest
objControl = New YL2PF
... etc
End Select
AddHandler objControl.ProgressEvent, AddressOf OnProgressEvent
objControl.DoSomething() ' <- lengthy process will raise ProgressEvent
....
Sub OnProgressEvent(sender as Object, e As ProgressEventArgs)
' Update your progress control - pay attention to "simon at rcl"'s comment above
' about making sure this is done on the GUI thread. You will have to use Invoke
' if the event will be fired from another thread.
' OP mentioned in a comment that they are using a BackgroundWorker so the
' ProgressEvent will be called in a background thread which can't change the
' UI. So you would call its ReportProgress which will in turn raise its own
' ProgressChanged event but on the UI thread.
backgroundWorker1.ReportProgress(CInt(e.Current / e.Maximum * 100))
End Sub
Private Sub backgroundWorker1_ProgressChanged( _
sender As Object, e As ProgressChangedEventArgs) _
Handles backgroundWorker1.ProgressChanged
' This will be called on the UI thread, so we can update the ProgressBar
Me.progressBar1.Value = e.ProgressPercentage
End Sub