Results 1 to 3 of 3

Thread: Run form with responsive control with no async functions

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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
  •  



Click Here to Expand Forum to Full Width