This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.
Do more with
Public Class Form1
WithEvents xworker As New System.ComponentModel.BackgroundWorker
Sub New()
Dim runGet As New SomeClass
With runGet
.id = 5
End With
xworker.RunWorkerAsync(runGet)
End Sub
Private Sub xWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles xworker.DoWork
Dim qWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Dim WC As SomeClass = CType(e.Argument, SomeClass)
WC.DoSomething(qWorker, e)
End Sub
Private Sub xWorker_Progress(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles xworker.ProgressChanged
'we expect a string as a progress return value which you can update the ui with
Dim ResString As String = e.UserState
End Sub
Private Sub xWorker_Completed(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles xworker.RunWorkerCompleted
'here we expect a list of string which you can update the ui with
Dim ResList As List(Of String) = e.Result
End Sub
End Class
Public Class SomeClassInsideYourModule
Public name As String = ""
Public id As Integer = 0
Public Sub DoSomething(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
worker.WorkerReportsProgress = True
'here test progress
name = "Progress Name"
worker.ReportProgress(0, name)
'here test result
name = "Result Name"
id = 15
e.Result = New List(Of String) From {name, id}
End Sub
End Class
Public Class AnalyzeFiles
Public sw As System.IO.StreamWriter
Public folder As String
Public Sub DoSomething(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
worker.WorkerReportsProgress = True
Dim NumberOfFiles As Integer = 0
Dim NumberOfErrors As Integer = 0
Dim erFile As String = Nothing
Dim dt As DateTime = DateTime.Today
Dim di As System.IO.DirectoryInfo
Dim foldersToProcess As New List(Of System.IO.DirectoryInfo)
foldersToProcess.Add(New System.IO.DirectoryInfo(folder))
While foldersToProcess.Count > 0
di = foldersToProcess(0)
foldersToProcess.RemoveAt(0)
Try
For Each fi As System.IO.FileInfo In di.GetFiles
erFile = fi.FullName
NumberOfFiles = NumberOfFiles + 1
sw.WriteLine(erFile)
Next
Catch ex As Exception
worker.ReportProgress(0, "Cannot access folder: " & erFile)
NumberOfErrors = NumberOfErrors + 1
End Try
For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
foldersToProcess.Add(subDi)
Next
End While
e.Result = New List(Of Integer) From {NumberOfFiles, NumberOfErrors}
End Sub
End Class
Next you will need to declare a background worker in your main form (or where-ever you want to invoke your routine from) and handle its events (i.e do_work, progress changed and completed). Here is the code:WithEvents xworker As New System.ComponentModel.BackgroundWorker
Then the handlers: Private Sub xWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles xworker.DoWork
Dim qWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Dim WC As AnalyzeFiles = CType(e.Argument, AnalyzeFiles)
WC.DoSomething(qWorker, e)
End Sub
Private Sub xWorker_Progress(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles xworker.ProgressChanged
'here you can update your textbox3
Textbox3.appendtext(e.UserState)
End Sub
Private Sub xWorker_Completed(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles xworker.RunWorkerCompleted
'since we expect a list of integer with NumberOfFiles at index 0 and NumberOfError at index 1
Dim ResList As List(Of Integer) = e.Result
Textbox4.text = ResList.First.ToString
Textbox5.text = ResList.Last.ToString
End Sub
Finally, to call the background worker, simply add the line:xWorker.RunWorkerAsync()
While foldersToProcess.Count > 0
....
Catch ex as Exception
worker.ReportProgress(0, "Cannot access folder: " & erFile)
End Try
....
worker.ReportProgress(10, New List(Of Integer) From {NumberOfFiles, NumberOfErrors}
)
End While
The simply change the progress handler to filter on the progress percentage, 0 for updating textbox3 and 10 for updating textboxes 4 and 5 (also bear in mind the different types being returned by each percentage, i.e string and list of integer respectively)
Public Class Form1
Private Sub btnSetStartFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnSetStartFolder.Click
Using fbd As New FolderBrowserDialog
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = fbd.SelectedPath
End If
End Using
End Sub
Private Sub btnSetOutpuFile_Click(sender As System.Object, e As System.EventArgs) Handles btnSetOutpuFile.Click
Using sfd As New SaveFileDialog
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox2.Text = sfd.FileName
End If
End Using
End Sub
Private Sub btnStartFindFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnStartFindFiles.Click
btnStartFindFiles.Enabled = False
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
Dim FF As New FindFiles(TextBox1.Text, TextBox2.Text)
AddHandler FF.FindFilesError, AddressOf FF_FindFilesError
AddHandler FF.FindFilesProgress, AddressOf FF_FindFilesProgress
AddHandler FF.FindFilesFailed, AddressOf FF_FindFilesFailed
AddHandler FF.FindFilesComplete, AddressOf FF_FindFilesComplete
FF.StartAnalyze()
End Sub
Private Sub FF_FindFilesError(FileName As String)
TextBox3.AppendText(FileName & vbCrLf)
End Sub
Private Sub FF_FindFilesProgress(NumberOfFiles As Integer, NumberOfErrors As Integer)
TextBox4.Text = NumberOfFiles
TextBox5.Text = NumberOfErrors
End Sub
Private Sub FF_FindFilesFailed(Failure As String)
MessageBox.Show(Failure, "FindFiles() Failed!")
End Sub
Private Sub FF_FindFilesComplete()
MessageBox.Show("Done!", "FindFiles() Complete!")
btnStartFindFiles.Enabled = True
End Sub
End Class
Public Class FindFiles
Private _OutputPath As String = ""
Private _SelectedFolderPath As String = ""
Private T As System.Threading.Thread = Nothing
Private SC As System.Threading.SynchronizationContext = Nothing
Public Event FindFilesError(ByVal FileName As String)
Public Event FindFilesProgress(ByVal NumberOfFiles As Integer, ByVal NumberOfErrors As Integer)
Public Event FindFilesFailed(ByVal Failure As String)
Public Event FindFilesComplete()
Private Sub New()
End Sub
Public Sub New(ByVal SelectedFolderPath As String, ByVal OutputPath As String)
_OutputPath = OutputPath
_SelectedFolderPath = SelectedFolderPath
SC = System.Windows.Forms.WindowsFormsSynchronizationContext.Current
End Sub
Public Sub StartAnalyze()
If IsNothing(T) Then
T = New System.Threading.Thread(AddressOf BackgroundProcess)
T.Start()
End If
End Sub
Private Sub BackgroundProcess()
Try
Using sw As New System.IO.StreamWriter(_OutputPath)
FindFiles(_SelectedFolderPath, sw)
End Using
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseFailed), ex.Message)
End If
End Try
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseComplete), Nothing)
End If
End Sub
Private Sub FindFiles(ByVal folder As String, ByVal sw As System.IO.StreamWriter)
Dim NumberOfFiles As Integer = 0
Dim NumberOfErrors As Integer = 0
Dim dt As DateTime = DateTime.Today
Dim di As System.IO.DirectoryInfo
Dim fi As System.IO.FileInfo
Dim foldersToProcess As New List(Of System.IO.DirectoryInfo)
Try
foldersToProcess.Add(New System.IO.DirectoryInfo(folder))
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseFailed), ex.Message)
End If
Exit Sub
End Try
While foldersToProcess.Count > 0
di = foldersToProcess(0)
foldersToProcess.RemoveAt(0)
Try
For Each fi In di.GetFiles
NumberOfFiles = NumberOfFiles + 1
sw.WriteLine(fi.FullName)
Next
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseError), di.FullName)
End If
NumberOfErrors = NumberOfErrors + 1
End Try
Try
foldersToProcess.AddRange(di.GetDirectories)
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseError), di.FullName)
End If
End Try
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseProgress), New Integer() {NumberOfFiles, NumberOfErrors})
End If
End While
End Sub
Private Sub RaiseError(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesError(state)
End Sub
Private Sub RaiseProgress(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
Dim values() As Integer = CType(state, Integer())
RaiseEvent FindFilesProgress(values(0), values(1))
End Sub
Private Sub RaiseFailed(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesFailed(state.ToString)
End Sub
Private Sub RaiseComplete(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesComplete()
End Sub
End Class
Premium Content
You need an Expert Office subscription to comment.Start Free Trial