0

I have this method what called from an Async method

private void SocketConnected(Object sender, EventArgs e) {
    //Some code


    connectedLabel.Content = "Connected";
}

And the connectedLabel.Content = "Connected"; throws an error. How can I update this from this method? I've seen a few post saying use Invoke, however, that gives me an error that label doesn't have a Invoke method.

EDIT:

From looking at Dispatcher as mentioned in the comments I've got this which works.

    private string message;
    public void UpdateConnected()
    {
        connectedLabel.Content = message;
    }

    private void SocketConnected(Object sender, EventArgs e) {
        //Some stuff

        message = "Connected";
        this.Dispatcher.BeginInvoke(new Action(this.UpdateConnected), DispatcherPriority.Background);
    }

Is that the correct usage of the Dispatcher?

12
  • Can you show us how you're calling this? If you're calling it from an async method.. you should be await'ing a result.. Commented Feb 26, 2014 at 22:06
  • 2
    What UI toolkit are you using? What Invoke-like method to use depends on the UI toolkits as e.g. WinForms and WPF provide different means of synchronizing oneself back into the UI thread. Add an appropriate tag, please. Commented Feb 26, 2014 at 22:06
  • 1
    @SimonWhitehead This is using event based asynchrony, not task based asynchrony. Commented Feb 26, 2014 at 22:07
  • Oh, so it is (you would think the method signature would make me realise that). My mistake. Commented Feb 26, 2014 at 22:08
  • 1
    @TomHart It's still important for us to know what UI framework you're using. Commented Feb 26, 2014 at 22:09

3 Answers 3

1

Regarding your edit, unless you have a need for the async method to carry on with whatever it has left to do before the label is updated, you can just use Dispatcher.Invoke rather than Dispatcher.BeginInvoke.

If you're interested in the difference: Dispatcher Invoke(...) vs BeginInvoke(...) confusion

Sign up to request clarification or add additional context in comments.

1 Comment

That's cool to know about Invoke vs BeginInvoke, thanks for the link.
1

You can create an extension method and use async/await

await socket.ConnectTaskAsync(host,port);
connectedLabel.Content = "Connected";

public static class SocketExtensions
{
    public static Task ConnectTaskAsync(this Socket socket, string host, int port)
    {
        return Task.Factory.FromAsync(
                         socket.BeginConnect(host, port, null, null),
                         socket.EndConnect);
    }
}

1 Comment

The OP appears to be using an event based asynchronous model, which won't work with FromAsync.
0

Why not just... ?

// WPF
Dispatcher.BeginInvoke(new Action(() => label.Content = "my label"));

or

// WinForms
BeginInvoke(new Action(() => label.Text = "my label"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.