简体   繁体   中英

Async/await task issue

I have this code and I want to run in the background, but I can not do. The GUI is frozen until the end. What is the error? I tried anyway, but I still do. I have code that works, but not for this example: S Thx!

private RelayCommand _jugarCommand;
    public RelayCommand JugarCommand
    {
        get
        {
            return _jugarCommand ??
                   (_jugarCommand =
                       new RelayCommand(Jugar,
                           () => CantidadDeSimulaciones != null && Regex.IsMatch(CantidadDeSimulaciones, @"^\d+$")));
        }
    }  



private async void Jugar()
    {

        Jugadas = await CalcularJugadas();

        GanoA += Jugadas.Count(x => x.QuienGano == "JUGADOR A");
        GanoB += Jugadas.Count(x => x.QuienGano == "JUGADOR B");
        Empate += Jugadas.Count(x => x.QuienGano == "EMPATE");
        JugadasAcumuladas += 1;

    }

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

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

                var numeroAleatorio = new Random();

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

                    var randomNumberA = numeroAleatorio.NextDouble();
                    var randomNumberB = numeroAleatorio.NextDouble();
                    var queEligioA = QueEligio(randomNumberA).ToString();
                    var queEligioB = QueEligio(randomNumberB).ToString();

                    var nuevoJuegada = new Jugada
                    {
                        ID = iIndex,
                        NumeroAleatorioA = randomNumberA,
                        NumeroAleatorioB = randomNumberB,
                        JugadorA = new Jugador
                        {
                            QueEligio = queEligioA
                        },
                        JugadorB = new Jugador
                        {
                            QueEligio = queEligioB
                        },
                        QuienGano = QuienGano(queEligioA, queEligioB)
                    };

                    calcularJugadas.Add(nuevoJuegada);
                }

                return calcularJugadas;
            }
            );
    }

You are not applying the concept of async properly. Change your CalcularJugadas to async first.

Also you might want to change the Task.Run to TaskFactory.StartNew for better multithreading. (Note that startnew is awaitable). Your function will look like this:

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

    return  await TaskFactory.StartNew(
        () =>
        {
            var calcularJugadas = new ObservableCollection<Jugada>();

            var numeroAleatorio = new Random();

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

                var randomNumberA = numeroAleatorio.NextDouble();
                var randomNumberB = numeroAleatorio.NextDouble();
                var queEligioA = QueEligio(randomNumberA).ToString();
                var queEligioB = QueEligio(randomNumberB).ToString();

                var nuevoJuegada = new Jugada
                {
                    ID = iIndex,
                    NumeroAleatorioA = randomNumberA,
                    NumeroAleatorioB = randomNumberB,
                    JugadorA = new Jugador
                    {
                        QueEligio = queEligioA
                    },
                    JugadorB = new Jugador
                    {
                        QueEligio = queEligioB
                    },
                    QuienGano = QuienGano(queEligioA, queEligioB)
                };

                calcularJugadas.Add(nuevoJuegada);
            }

            return calcularJugadas;
        }
        );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM