-
Jan 9th, 2021, 02:43 PM
#1
Run form with responsive control with no async functions
Hi.
I have a form and I want it's controls to respond while running a long function.
The function is synchronous and I do not have any async functions to call at it.
I needed to make the interface responsive and of course without doevents().
I tried a background worker but as soon as it stepped to a ProgressBar form control it crashed.
I don't wont to have to handle the controls on the backgroundworker performtask , that request a complete remake of the form
I was also struggling on making it a task so I could run it. I did not know the exact steps so i skipped that and I fount this solution:
Code:
Dim MyThread As New Thread(AddressOf MyFunction)
MyThread .IsBackground = True
MyThread .Start()
Now I have a method that i found that will invoke any control:
Code:
Public Module Extensions
''' <summary>
''' Invokes the specified method on the calling control's thread (if necessary, otherwise on the current thread).
''' </summary>
''' <param name="Control">The control which's thread to invoke the method at.</param>
''' <param name="Method">The method to invoke.</param>
''' <param name="Parameters">The parameters to pass to the method (optional).</param>
''' <remarks></remarks>
<Extension()>
Public Function InvokeIfRequired(ByVal Control As Control, ByVal Method As [Delegate], ByVal ParamArray Parameters As Object()) As Object
If Parameters IsNot Nothing AndAlso
Parameters.Length = 0 Then Parameters = Nothing
If Control.InvokeRequired = True Then
Return Control.Invoke(Method, Parameters)
Else
Return Method.DynamicInvoke(Parameters)
End If
End Function
End Module
And on any control I use this:
Code:
Me.InvokeIfRequired(Sub()
RichTextBox2.SelectionColor = Color.Red
RichTextBox2.AppendText("test" & vbCrLf)
RichTextBox2.SelectionColor = Color.Black
Button1.Enabled = True
ProgressBar1.Visible = true
ProgressBar1.Value = 1
End Sub)
''''strangely this does not require invoke:
For Each row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow).Where(Function(x) x.Cells("Online").Value = True And x.Cells("Select").Value = True)
''''
etc
2 Questions before I start recreating the functions:
1)Is this a viable solution?
2)How would I know which control requires an InvokeIfRequired?
The datagrid rows do not as show above but if I try to AppendText to a string from a RTB it will pop.
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 9th, 2021, 09:58 PM
#2
Re: Run form with responsive control with no async functions
If you happen to be using VS2019 I have a code sample that shows how to keep an application responsive.
There are two projects, a class project and a forms project on GitHub.
The objective for these projects to demonstrate a recursive method for iterating large complex folder structures while keeping the app interface completely responsive and the methods are cancelable.
To download only those two projects, create a batch file and place the contents below in and run (requires Git).
Code:
mkdir code
cd code
git init
git remote add -f origin https://github.com/karenpayneoregon/vb-vs2019-samples
git sparse-checkout init --cone
git sparse-checkout add FileHelpers
git sparse-checkout add RecurseFolders
git pull origin master
:clean-up
del .gitattributes
del .gitignore
del .yml
del .editorconfig
del *.md
del *.sln
And here is a GitHub project that might be easier to follow.
-
Jan 10th, 2021, 12:28 PM
#3
Re: Run form with responsive control with no async functions
Hi Karen.
Unfortunately only as far as vs 2015, I don't think the work PC can handle a forth Visual Studio, let alone a new 2019.
Is there a vb file I can just read so I can understand or an example?
I'm just browsing and I say an example you have that looks like:
Code:
Private Sub ExceptionHappened(sender As Exception)
ExceptionsListBox.InvokeIfRequired(
Sub(listBox)
listBox.Items.Add(sender.Message)
listBox.SelectedIndex = listBox.Items.Count - 1
End Sub)
End Sub
That look almost identical on what I do in the original post. So are you just running async tasks instead of threads?
Thanks.
Last edited by sapator; Jan 10th, 2021 at 12:32 PM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|