mad*_*lan 4 .net vb.net events class event-handling
我正在尝试将一些代码移动到某些类文件中以清理我的代码.我遇到问题的一个方面是报告执行任务的对象和进度条之间事件的进度.
我想事件函数必须放在新类中,但是他们还需要更新调用表单上的进度条?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
Run Code Online (Sandbox Code Playgroud)
用于:
DoRestore(SQLServer, "C:\SQLBACKUP.bak", DatabaseName)
Run Code Online (Sandbox Code Playgroud)
Mat*_*lko 10
您应该在类中定义一个事件并在表单中处理进度条更新(假设WinForms?) - 这里的重点是该类关注备份某些东西 - 它不应该有任何进度条的概念:
在类中定义事件:
Public Event ReportProgress(byval progress as integer)
Run Code Online (Sandbox Code Playgroud)
在执行备份时根据需要引发此事件:
RaiseEvent ReportProgress(value)
Run Code Online (Sandbox Code Playgroud)
在您需要的调用代码中
使用WithEvents以下方法定义类:
Private WithEvents Backup As BackupClass
Run Code Online (Sandbox Code Playgroud)
然后对事件采取行动:
Private Sub Backup _ReportProgress(progress As Integer) Handles Backup.ReportProgress
Debug.WriteLine("Progress:" + progress.ToString)
End Sub
Run Code Online (Sandbox Code Playgroud)或手动添加处理程序:
Private Sub Backup_ReportProgressHandler(progress As Integer)
Debug.WriteLine("Progress Handler:" + progress.ToString)
End Sub
AddHandler Backup.ReportProgress, AddressOf Backup_ReportProgressHandler
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
7465 次 |
| 最近记录: |