Question

I have a Winform GUI that have an UDPClient listener thread working on second plane to avoid halt the GUI; when the thread receive something call a Sub in the WinForm code; that Sub process the data and must to fill different TextBox depending on the received data.

As you know if I try to change the text property of any control from the Sub I will receive a Cross Thread error. So in order to avoid that I make some delegates for a few control(just a test) and works OK.

But the WinForm have more than 100 controls and I was wondering if there's some way to do it with less code.

Making a search I found this two questions.

Multi-threaded WPF Application: Dispatcher Invoke. A more efficient way?

Change WPF controls from a non-main thread using Dispatcher.Invoke

On the first link they talk about use Dispacher, something like

Public Shared Sub UiInvoke(a As Action)
    Application.Current.Dispatcher.Invoke(a)
End Sub

I wrote that code in my Form but the IDE(VS2010/NET4.0) says that "Current" is not a member.

I think I'm missing something, I never used before or do something similar as Dispacher, I usually use Delegates.

What I'm doing wrong? There's another way to control many controls with one Delegate? I just need to read or write the text property.

Was it helpful?

Solution

You can use anonymous Subs:

Me.Invoke(
    Sub()
        ' Update controls here
    End Sub)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top