#Region "TabPage_11"
Private mEditor As ControlTextEditor
Private mLabelStateText_11 As String
Private mSb__11 As New StringBuilder
Friend WithEvents mBackgroundWorker_11 As System.ComponentModel.BackgroundWorker
Private Sub ChangeCount_11(c As Integer)
Label_Count_11.Text = CStr(c)
End Sub
Private Sub Button_RunBackgroundWorker_11_Click(sender As Object, e As System.EventArgs) Handles Button_RunBackgroundWorker_11.Click
'Runs on main thread
'BackgroundWorker_11 DoWork event is the result of calling RunWorkerAsync
If mBackgroundWorker_11 Is Nothing Then
mBackgroundWorker_11 = New System.ComponentModel.BackgroundWorker()
mBackgroundWorker_11.WorkerReportsProgress = True
mBackgroundWorker_11.WorkerSupportsCancellation = True
End If
If Not mBackgroundWorker_11.IsBusy Then
If Not mBackgroundWorker_11.CancellationPending Then
Button_CancelBackgroundWorker_11.Enabled = True
mLabelStateText_11 = "INITIALIZING"
Label_Count_11.Text = "0"
Label_Max_11.Text = "0"
mCount_11 = 0
mSb__11.Clear()
'Cause BackgroundWorker to raise the DoWork event: mBackgroundWorkerDoWorkHandler
mBackgroundWorker_11.RunWorkerAsync(ControlRichTextBox_11)
'Since I included an argument, RunWorkerAsync() will construct a DoWorkEventArgs object containing the argument,
'Invoke the DoWork delegate asynchronously, And pass it the DoWorkEventArgs object as e And itself as sender.
Else
mLabelStateText_11 = "CAN NOT START - CANCEL PENDING"
End If
Else
mLabelStateText_11 = "BACKGROUND DID NOT START"
End If
UpdateLableStateText()
End Sub
'If Invoke is required it invokes itself and for some reason it is then entered with InvokeRequired=False
'I need to check to see: maybe when a sub is invoked, InvokeReuired is always false. I think that does makes sense.
Private Sub UpdateUI() 'form method from JohnH
If InvokeRequired Then
Invoke(Sub() UpdateUI())
Exit Sub
End If
Static z As Integer = 0
Label2.Text = CStr(z)
z += 1
End Sub
Private mCount_11 As Integer
Private mSourceFiePaths As String()
Private Sub BackgroundWorker_11_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles mBackgroundWorker_11.DoWork
'Runs on BacgroundWorker Thread
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
'Debug.WriteLine(ReferenceEquals(mBackgroundWorker_11, worker)) ' Shows True, so worker Not really needed here
mEditor = DirectCast(e.Argument, ControlTextEditor)
'Invoke a function on the UI thread
'If the method will only be called from a non-GUI thread there's no point in using InvokeRequired
mSourceFiePaths = DirectCast(ControlFolderAndFileExplorer_1_11.Invoke(Function() ControlFolderAndFileExplorer_1_11.qGetFileFullPathsFromSelectedFoldersTree), String())
If mSourceFiePaths Is Nothing Then
CancelThread()
Exit Sub
End If
mLabelStateText_11 = "RUNNING"
UpdateLableStateText()
mBackgroundWorker_11.ReportProgress(1)
Button_CancelBackgroundWorker_11.Enabled = True
For Each fileFullPath As String In mSourceFiePaths
If mBackgroundWorker_11.CancellationPending Then
Exit Sub
End If
UpdateUI()
Try
'Run this sub on worker thread
mSb__11.AppendLine(fileFullPath)
mCount_11 += 1
UpdateLableStateText() 'As it is it would slow the process
Catch
End Try
Next
End Sub
Private Sub BackgroundWorker_11_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles mBackgroundWorker_11.ProgressChanged
'Runs on main thread
' BackgroundWorker_11.ProgressChanged raised by running mBackgroundWorker.ReportProgress
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
'Debug.WriteLine(mBackgroundWorker_11 Is worker) 'Shows True
UpdateLableStateText()
End Sub
Private Sub Button_CancelBackgroundWorker_11_Click(sender As Object, e As EventArgs) Handles Button_CancelBackgroundWorker_11.Click
'Runs on main thread
CancelThread()
End Sub
Private Sub CancelThread()
If mBackgroundWorker_11.WorkerSupportsCancellation Then
If Not mBackgroundWorker_11.CancellationPending Then
mLabelStateText_11 = "CANCEL PENDING"
mBackgroundWorker_11.CancelAsync()
End If
End If
UpdateLableStateText()
End Sub
Delegate Sub SetTextCallbackDelegateType(ByVal text As String)
Private Sub SetText(ByVal text As String)
mEditor.TxtText = text
End Sub
Private Sub mBackgroundWorker_11_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles mBackgroundWorker_11.RunWorkerCompleted
'Runs on Main thread
mLabelStateText_11 = "PREPARING TO DISPLAY"
UpdateLableStateText()
mEditor.TxtText = mSb__11.ToString
mLabelStateText_11 = "DONE"
UpdateLableStateText()
'Using the Control.Invoke() method you may move the execution of a method or function from a background thread to the thread that the control was created on, which is usually the UI
'A lambda expressions can be used to create a delegate method on the fly:
'Control.Invoke Method Info
'Invoke(Delegate) Executes the specified delegate on the thread that owns the control's underlying window handle.
'Delegate A delegate to a method that takes no parameters and contains a method to be called in the control's thread context.
'Returns an object value from the delegate being invoked, or null if the delegate has no return value.
'Invoke(Delegate, Object()) Executes the specified Delegate, On the thread that owns the control's underlying window handle, with the specified list of arguments.
'A delegate to a method that takes parameters of the same number and type that are contained in the args parameter and contains a method to be called in the control's thread context.
'Object()
'An Array of objects to pass as arguments to the specified method. This parameter can be null if the method takes no arguments.
'Returns An Object that contains the return value from the delegate being invoked, or null if the delegate has no return value.
End Sub
Private Sub UpdateLableStateText()
If mSourceFiePaths Is Nothing Then Exit Sub
'If the method will only be called from a non-GUI thread there's no point in using InvokeRequired
'But this method is called from both threads by design
'Accessing a sub with parameters
'Without using a lambda expression creating a Delegate Is required
If mEditor.InvokeRequired Then
Dim setTextCallbackDelegateObject As New SetTextCallbackDelegateType(AddressOf SetText)
mEditor.Invoke(setTextCallbackDelegateObject, New Object() {mSb__11.ToString})
Else
mEditor.TxtText = mSb__11.ToString
End If
'Accessing a sub with parameters
If Me.InvokeRequired Then
'A lambda expressions used to create a delegate method on the fly:
Me.Invoke(Sub() ChangeCount_11(mCount_11))
Else
Me.Invoke(Sub() ChangeCount_11(mCount_11))
End If
If Me.InvokeRequired Then
'A lambda expressions used to create a delegate method on the fly:
'Accessing a set property
Me.Invoke(Sub() Label_Max_11.Text = CStr(mSourceFiePaths.Length))
Else
Me.Invoke(Sub() Label_Max_11.Text = CStr(mSourceFiePaths.Length))
End If
'Accessing a set property
If Label_State__11.InvokeRequired Then
'If called by the worker thread
Label_State__11.Invoke(Sub() Label_State__11.Text = mLabelStateText_11)
Else
'If called by the UI thread
Label_State__11.Text = mLabelStateText_11
End If
End Sub