2012-02-23 24 views
0

我的文件上傳函數用於顯示它在ProgressBar中的進度,但現在我已將它移到DLL中,因此不再可能。將控制權傳遞給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) 

是這樣的可能嗎?

+0

進度條應該在你的調用形式,所以DLL應該實現一個回調機制,通知主要調用者文件更新進度(可能傳入一個百分比作爲參數)。 – vulkanino 2012-02-23 11:09:40

+0

如何讓回調知道要「報告」什麼?我以前從未使用過。 – natli 2012-02-23 11:31:21

回答

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 

記住,當你完成還去除事件處理程序。

+0

工作得很好,謝謝! – natli 2012-02-26 20:15:59