-2
private void Build_Button(object sender, RoutedEventArgs e)
    {
        T.BuildStatus = "Building...";            
        BuildSelected(T.ListofTrucks);                  
        T.BuildStatus = "Build Complete";
    }

Currently I have a label binded to T.BuildStatus that I want to display "Building..." while my files are compiled but the label will only update after buildSelected method has finished. How can I get this label to update before continuing with the rest of the event call?

Edit: the object T is implementing INotifyPropertyChanged correctly

Kennya42
  • 11
  • 4
  • Step 1: Go MVVM and forget about your WinForms background. – hyankov Mar 02 '17 at 22:53
  • @HristoYankov I don't see any indication that they haven't... – Servy Mar 02 '17 at 22:54
  • I am using MVVM, I think that the event to update BuildStatus is being delayed until the button event completes. – Kennya42 Mar 02 '17 at 22:58
  • `BuildSelected(T.ListofTrucks);` looks as though it's being called on your UI thread blocking any and all UI updates. If its CPU bound kick it off the UI thread with `Task.Run` or make it `async` if its IO bound. – JSteward Mar 02 '17 at 22:59
  • @JSteward Could you explain a little more how to do that? I haven't done threading in wpf. – Kennya42 Mar 02 '17 at 23:05

1 Answers1

0

Let's assume BuildSelected(T.ListofTrucks); is CPU bound work. If it's called on the UI thread it will block INotify updates, i.e. since the UI thread is busy it can't update. Easiest fix:

private async void Build_Button(object sender, RoutedEventArgs e)
{
    T.BuildStatus = "Building...";            
    await Task.Run(() => BuildSelected(T.ListofTrucks));                  
    T.BuildStatus = "Build Complete";
}

Or if an IO operation:

private async void Build_Button(object sender, RoutedEventArgs e)
{
    T.BuildStatus = "Building...";            
    await BuildSelectedAsync(T.ListofTrucks));                  
    T.BuildStatus = "Build Complete";
}

async Task BuildSelectedAsync(ListofTrucks x) implementation will vary depending on the IO operation and what you're exactly doing.

JSteward
  • 6,833
  • 2
  • 21
  • 30