将控制权传递给DLL

时间:2012-02-23 11:05:05

标签: vb.net

我的文件上传功能用于显示它在ProgressBar中的进度,但现在我将其移动到DLL中已不再可能。

我想做这样的事情(在dll中):

Public Function uploadfile(ByVal name As String, ByVal path As String, ByVal identifier As Integer,ByVal control As ProgressBarThingyHere,ByVal toProgressBar As Boolean) As String
    'snipped unimportant code
    If toprogressbar then
        SendFileWithProggress(path,control)
    else
        SendFileNoProgress(path)
    end if
End Function

    'Send File
    Private Sub SendFileNoProgress(ByVal path As String)
        sendfile(path, NULL, False)
    End Sub

    Private Sub SendFileWithProggress(ByVal path As String, ByVal control As ProgressBarThingyHere)
        sendfile(path, control, True)
    End Sub

这样我就可以调用(伪代码)

dll.uploadfile("filename","path",fileID,Form1.ProgressBar1,true)

dll.uploadfile("filename","path",fileID,NULL,false)

这样的事情可能吗?

1 个答案:

答案 0 :(得分:3)

类似的东西:

Public Class YourUploadingClassInTheDLL
   Public Event BytesAreComing(ByVal percent As Integer)
   Public Event LoadingFinished()

   private Sub loader(Byval reportStatus as Boolean)

      Dim percent as Integer = 0
      ' load the file in a loop maybe
      Do
          ' ...

          if reportStatus Then
            ' report the percentage to the client by raising an event 
            RaiseEvent BytesAreComing(percent)
          End If
    While ( ... )

    RaiseEvent LoadingFinished()
   End Sub

   Public Sub LoadWithStatusBar()
      loader(True);
   End Sub

   Public Sub LoadWithOutStatusBar()
      loader(False);
   End Sub

End Class

在客户端代码中:

Private MySplendidDll as MyDLL

Public Sub Main
   MySplendidDll = New MyDLL
   AddHandler MySplendidDll.BytesAreComing, AddressOf BytesAreComingHandler
End Sub

 Private Sub BytesAreComingHandler(byval percent as integer)
   ' update the progress bar
 End Sub

请记住在完成后也删除事件处理程序。