BenSCooper
Programmer
Dear All,
I'm sure this is probably a simple enough question, but after many hours of searching I'm thoroughly confused and no nearer finding an answer.
Problem
I have a standard 3 tier (Pres\BLL\DAL) WPF\vb.net app that uses SMO to backup\restore a SQL database.
Once the user clicks the button I just want to display a progress bar showing the restore progress. I'm at the point where my restore function raises an event, which eventually sets the progress bar value, but the form doesn't update until the restore operation is completed.
I'm guessing that I need to run the restore in a new thread. I've looked at a lot of examples using the BackgroundWorker and delegates etc, but can't seem to translate these simple, all the code on one form, examples into working with a tiered architecture.
If anyone could provide some guidance to a multithreading newbie I'd be very grateful. See existing code below (for brevity I've only included the truncated button click event and the part of the data access that performs the restore):
Thanks
Ben
DAL LAYER
Public Class DAL_SMOBase
Public Event ProgressEvent(ByVal intPercentComplete As Integer)
Public Sub RestoreFullBackup(ByVal strBackupFile As String, ByVal strDatabase As String, Optional ByVal strMediaPassword As String = "")
Try
Dim objRestore As New Restore
objRestore.Database = strDatabase
objRestore.Action = RestoreActionType.Database
objRestore.Devices.AddDevice(strBackupFile, DeviceType.File)
objRestore.ReplaceDatabase = True
If Not String.IsNullOrWhiteSpace(strMediaPassword) Then objRestore.SetMediaPassword(strMediaPassword)
objRestore.PercentCompleteNotification = 10
AddHandler objRestore.PercentComplete, AddressOf ProgressEventHandler
objRestore.SqlRestoreAsync(objServer)
Catch ex As Exception
Throw New System.Exception(ex.Message, ex.InnerException)
End Try
End Sub
Private Sub ProgressEventHandler(ByVal sender As Object, ByVal e As PercentCompleteEventArgs)
RaiseEvent ProgressEvent(CType(e.Percent, Integer))
End Sub
PRESENTATION LAYER
Private Sub RestoreBackup_cmdRestore_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RestoreBackup_cmdRestore.Click
Dim clsSQLBackupRestore As New BLL_SQLBackupRestore
AddHandler clsSQLBackupRestore.ProgressEvent, AddressOf UpdateProgressBar
clsSQLBackupRestore.RestoreFullBackup(strRestoreFile)
End Sub
Private Sub UpdateProgressBar(ByVal intPercentComp As Integer)
ProgressBar1.Value = intPercentComp
End Sub
I'm sure this is probably a simple enough question, but after many hours of searching I'm thoroughly confused and no nearer finding an answer.
Problem
I have a standard 3 tier (Pres\BLL\DAL) WPF\vb.net app that uses SMO to backup\restore a SQL database.
Once the user clicks the button I just want to display a progress bar showing the restore progress. I'm at the point where my restore function raises an event, which eventually sets the progress bar value, but the form doesn't update until the restore operation is completed.
I'm guessing that I need to run the restore in a new thread. I've looked at a lot of examples using the BackgroundWorker and delegates etc, but can't seem to translate these simple, all the code on one form, examples into working with a tiered architecture.
If anyone could provide some guidance to a multithreading newbie I'd be very grateful. See existing code below (for brevity I've only included the truncated button click event and the part of the data access that performs the restore):
Thanks
Ben
DAL LAYER
Public Class DAL_SMOBase
Public Event ProgressEvent(ByVal intPercentComplete As Integer)
Public Sub RestoreFullBackup(ByVal strBackupFile As String, ByVal strDatabase As String, Optional ByVal strMediaPassword As String = "")
Try
Dim objRestore As New Restore
objRestore.Database = strDatabase
objRestore.Action = RestoreActionType.Database
objRestore.Devices.AddDevice(strBackupFile, DeviceType.File)
objRestore.ReplaceDatabase = True
If Not String.IsNullOrWhiteSpace(strMediaPassword) Then objRestore.SetMediaPassword(strMediaPassword)
objRestore.PercentCompleteNotification = 10
AddHandler objRestore.PercentComplete, AddressOf ProgressEventHandler
objRestore.SqlRestoreAsync(objServer)
Catch ex As Exception
Throw New System.Exception(ex.Message, ex.InnerException)
End Try
End Sub
Private Sub ProgressEventHandler(ByVal sender As Object, ByVal e As PercentCompleteEventArgs)
RaiseEvent ProgressEvent(CType(e.Percent, Integer))
End Sub
PRESENTATION LAYER
Private Sub RestoreBackup_cmdRestore_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RestoreBackup_cmdRestore.Click
Dim clsSQLBackupRestore As New BLL_SQLBackupRestore
AddHandler clsSQLBackupRestore.ProgressEvent, AddressOf UpdateProgressBar
clsSQLBackupRestore.RestoreFullBackup(strRestoreFile)
End Sub
Private Sub UpdateProgressBar(ByVal intPercentComp As Integer)
ProgressBar1.Value = intPercentComp
End Sub