0

When trying to make a long call on a WPF MVVM project, the interface gets locked during the call. I can't figure out why...

Here's a exemple:

public class UIViewModel : ViewModelBaseClass
{    
...
    public ICommand Bt_Search { get; set; }
    
    public UIViewModel 
    {
        Bt_Search = new RelayCommand(o => Load());
    } 
    ...

    ...
    private async Task Load()
    {
         await Task.Run(() => Thread.Sleep(20000)); //just simulate a long call
    }
    ...
}

Here is my RelayCommmand Class:

public class RelayCommand : ICommand
{
    private Action<object> _action;

    public RelayCommand(Action<object> action)
    {
        _action = action;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _action(parameter);
    }

    public event EventHandler CanExecuteChanged;
}

And here my ViewModelBaseClass:

public abstract class ViewModelBaseClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
            return false;
        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }
}

When using an async Task on MVVM, shouldn't it leave the UI thread free? But it seems to be locking the thread, even when I use ConfigAwait(false).

Any help is appreaciated.

JWP
  • 6,672
  • 3
  • 50
  • 74

0 Answers0