Handling events in another class

I am trying to move some code to some class files in order to clear my code. In one area that I'm having problems with, it reports the progress of events between the object performing the task and the progress bar.

I suppose that the event functions should be placed in a new class, but they also need to update the progress bar in the calling form? Can a class \ object return updates instead of event handlers?

Currently the form has all the code:

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 

Used through:

 DoRestore(SQLServer, "C:\SQLBACKUP.bak", DatabaseName) 
+4
source share
2 answers

You must define an event in your class and handle updating the progress bar in the form (provided that WinForms?) - the point here is that the class is backing up something - it should not have a concept of progress bars:

Define an event in the class:

 Public Event ReportProgress(byval progress as integer) 

Raise this event if necessary when backing up:

 RaiseEvent ReportProgress(value) 

In the calling code you need to either

  • Define a class using WithEvents :

     Private WithEvents Backup As BackupClass 

    and then act on the event:

     Private Sub Backup _ReportProgress(progress As Integer) Handles Backup.ReportProgress Debug.WriteLine("Progress:" + progress.ToString) End Sub 
  • Or add a handler manually:

     Private Sub Backup_ReportProgressHandler(progress As Integer) Debug.WriteLine("Progress Handler:" + progress.ToString) End Sub AddHandler Backup.ReportProgress, AddressOf Backup_ReportProgressHandler 
+10
source

Well, you can do it, but to be honest, I find it less confusing if things like event handlers that update the progress bar of the form are in that form. Otherwise, to save it later (for example, to fix the problem with the execution step), now I need to go on an expedition to find out where you hid it.

So IMO, if the form calls a class that does something, and this class returns progress notifications, it is recommended that you process these notifications in the calling form.

+2
source

All Articles