MahApps.Metro icon indicating copy to clipboard operation
MahApps.Metro copied to clipboard

Great problem with async/await

Open avechuche opened this issue 11 years ago • 3 comments

Hi, I am doing a simple simulation of the classic game "rock-paper-scissors". The problem when using the "Styles / Controls.xaml" file and load a ListView from an asynchronous method or not, the GUI is frozen until the loop ends. If I use the default WPF controls, I do not have this problem. Any ideas?

avechuche avatar Jun 17 '15 01:06 avechuche

I suppose this is a bug in your code rather than a MahApps.Metro problem. If the GUI freezes while using an asychronous method, you might want to try data binding.

Dotnetifier avatar Jun 19 '15 11:06 Dotnetifier

Thanks for answering.

  1. If I use the default controls for WPF, I do not have that problem.
  2. I solved for now, adding to the ListView "Style = {x: Null}" but do not think is the most optimal.

This is a part of my code

private RelayCommand _jugarCommand; public RelayCommand JugarCommand { get { return _jugarCommand ?? (_jugarCommand = new RelayCommand(Jugar)); } }

private async void Jugar() { Jugadas = await CalcularJugadas(); }

private Task<ObservableCollection<Jugada>> CalcularJugadas()
{

    return Task.Run(
        () =>
        {
            var calcularJugadas = new ObservableCollection<Jugada>();

            for (var iIndex = 1; iIndex <= Convert.ToInt16(CantidadDeSimulaciones); iIndex++)
            {

            }
            return calcularJugadas;
        }
        );
}

avechuche avatar Jun 19 '15 15:06 avechuche

@avechuche You can load data in Jugadas using a async background worker.

see https://github.com/MahApps/MahApps.Metro/issues/2000 for background worker code.

deepak-rathi avatar Jul 01 '15 12:07 deepak-rathi