locked
WinForm alternative for WPF - Await Dispatcher.InvokeAsync(AddressOf selectedSessionChanged) RRS feed

  • Question

  • Hi

    Converting some code from a WPF GUI to my WinForm GUI and mostly all working fine

    But it uses line similar to below a few times:-

    Await Dispatcher.InvokeAsync(AddressOf selectedSessionChanged)

    Which gives error of 

    Error	BC30469	Reference to a non-shared member requires an object reference.

    What is the equivalent in a Winform project please

    Thanks


    Darren Rose


    • Edited by wingers Sunday, January 19, 2020 8:07 PM
    Sunday, January 19, 2020 8:03 PM

Answers

  • Await Dispatcher.InvokeAsync(AddressOf selectedSessionChanged)

    Hi Rose,
    in Form-class you can use instead of Dispatcher.InvokeAsync:

        Await Task.Factory.StartNew(Sub()
                                      Me.Invoke(New Action(AddressOf selectedSessionChanged))
                                    End Sub)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Sunday, January 19, 2020 9:36 PM
    • Marked as answer by wingers Sunday, January 19, 2020 11:59 PM
    Sunday, January 19, 2020 9:17 PM

All replies

  • Await Dispatcher.InvokeAsync(AddressOf selectedSessionChanged)

    Hi Rose,
    in Form-class you can use instead of Dispatcher.InvokeAsync:

        Await Task.Factory.StartNew(Sub()
                                      Me.Invoke(New Action(AddressOf selectedSessionChanged))
                                    End Sub)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Sunday, January 19, 2020 9:36 PM
    • Marked as answer by wingers Sunday, January 19, 2020 11:59 PM
    Sunday, January 19, 2020 9:17 PM
  • I like the idea of a repeatable pattern via a language extension if the intent is to work from a async method to the current thread.

    Public Module ControlExtensions
        <Runtime.CompilerServices.Extension>
        Public Sub InvokeIfRequired(Of T As ISynchronizeInvoke)(control As T, action As Action(Of T))
            If control.InvokeRequired Then
                control.Invoke(New Action(Sub() action(control)), Nothing)
            Else
                action(control)
            End If
        End Sub
    End Module

    Used in an async method on a DataGridView

    CustomersGrid.InvokeIfRequired(
        Sub(d)
            d.DataSource = _dtPeople
        End Sub)

    On a TextBox

    TextBox1.InvokeIfRequired(
        Sub(tb)
            tb.Text = _dtPeople.Rows(0).Field(Of String)("FirstName")
        End Sub)
    Etc.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, January 19, 2020 9:34 PM
  • Thanks Peter - that seems to work well for me

    Darren Rose

    Sunday, January 19, 2020 9:45 PM
  • Hi Karen

    Yes I use something similar to access controls across threads, but my question had nothing to do with controls


    Darren Rose

    Sunday, January 19, 2020 9:46 PM
  • Hi Karen

    Yes I use something similar to access controls across threads, but my question had nothing to do with controls


    Darren Rose

    Darren, can you be more specific as Dispatcher is only for WPF (no direct equivalent class). Are you looking for something to do with property changes or something else?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, January 19, 2020 9:51 PM
  •  as Dispatcher is only for WPF (no direct equivalent class).

    Yes I know hence my question as I was converting a project from WPF to WinForm and needed to work out how to do the equivalent of the code I posted in intial post.

    It was simply calling another sub - Private Sub selectedSessionChanged() - which proceses some data, nothing special about what it is calling, just the way it was called was in a WPF app and I needed it to work in a WinForm app - to be fair I thought initial question stated that quite clearly.


    Darren Rose

    Sunday, January 19, 2020 9:55 PM
  • Okay I see.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, January 19, 2020 9:57 PM