我試圖將一些代碼移入某些類文件以清理我的代碼。我遇到的一個問題是報告執行任務的對象與進度條之間的事件進度。在另一個類中處理事件
我想事件函數必須放在新類中,但他們也需要更新調用窗體上的進度條? class \ object可以返回更新來代替事件處理程序嗎?
目前的形式具有所有代碼:
Function DoRestore(ByVal SQLServer As String, ByVal BackupFilePath As String, ByVal DatabaseName As String)
Dim Server As Server = New Server(SQLServer)
Server.ConnectionContext.ApplicationName = Application.ProductName
Dim res As Restore = New Restore()
Dim dt As DataTable
res.Devices.AddDevice(BackupFilePath, DeviceType.File)
dt = res.ReadFileList(Server)
res.Database = DatabaseName
res.PercentCompleteNotification = 1
AddHandler res.PercentComplete, AddressOf RestoreProgressEventHandler
AddHandler res.Complete, AddressOf RestoreCompleteEventHandler
res.SqlRestoreAsync(Server)
While res.AsyncStatus.ExecutionStatus = ExecutionStatus.InProgress
Application.DoEvents()
End While
End Function
Private Function RestoreProgressEventHandler(ByVal sender As Object, ByVal e As PercentCompleteEventArgs)
'Update progress bar (e.Percent)
End Function
Private Sub RestoreCompleteEventHandler(ByVal sender As Object, ByVal e As Microsoft.SqlServer.Management.Common.ServerMessageEventArgs)
'Signal completion
End Sub
通過使用:
DoRestore(SQLServer, "C:\SQLBACKUP.bak", DatabaseName)
你會用這段代碼讓自己陷入相當深的麻煩。檢查這個答案:http://stackoverflow.com/questions/5181777/c-application-doevents/5183623#5183623 –