Mike Guthrie
Mike Guthrie

Reputation: 4059

How to force control content redraw within event handler

This task should be quite simple, but nothing I tried has worked. I'm just trying to get the text to go away when the button is clicked, and while data is loaded and validated.

I've tried the following, which doesn't force a redraw:

private delegate void InlineDelegate();
private void btnLogon_Click(object sender, RoutedEventArgs e)
{
    lblInvalidLogon.Dispatcher.Invoke(new InlineDelegate(() =>
    {
        lblInvalidLogon.Content = string.Empty;
        lblInvalidLogon.InvalidateVisual();
    }), System.Windows.Threading.DispatcherPriority.Render, null);
    //
    // Process to verify logon credentials...
    //
}

I've also tried DispatcherPriority.Send, and I've placed a Thread.Sleep following the Invoke to give it plenty of time to update the UI, but nothing I've tried will force the UI to update while the authentication process continues.

Upvotes: 1

Views: 667

Answers (1)

Mike Guthrie
Mike Guthrie

Reputation: 4059

Phil correctly pointed out the cause:

You need to move the logon verification into another thread, it's blocking the UI thread.

Was working on this, and came up with the solution right at the same time as his comment. I've solved it as below:

private delegate void InlineDelegate();
private void btnLogon_Click(object sender, RoutedEventArgs e)
{
    lblInvalidLogon.Content = string.Empty;
    lblInvalidLogon.Dispatcher.Invoke(new InlineDelegate(() =>
    {
        //
        // Process to verify logon credentials...
        //
    }), System.Windows.Threading.DispatcherPriority.Background, null);
}

Upvotes: 1

Related Questions