简体   繁体   中英

Async still freezes ui

I have never fully udnerstood async programming. I have this method:

internal static Task gpUpdate(string pcName, string localUser, string localPass)
        {
            return Task.Run(() =>
            {
                string result = "";
                string command = @"/C psexec \\" + pcName + " /accepteula -s Gpupdate.exe /force";
                // string command = "/C winrs -r:" + pcName + " ECHO ON&echo N|Gpupdate.exe /force";
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.FileName = "CMD.exe";
                process.StartInfo.WorkingDirectory = @"C:\Windows\System32";
                process.StartInfo.Arguments = command;
                process.StartInfo.UserName = Credentials.username;
                process.StartInfo.Password = Credentials.securePassword;
                process.StartInfo.Domain = "EPCE001N";
                process.Start();

                var output = process.StandardOutput.ReadToEnd();

                if (output.Contains("Aktualizace zásady uživatele byla úspěšně dokončena."))
                {
                    result = result + Environment.NewLine + pcName + " user GP update success...";
                }
                else
                {
                    result = result + Environment.NewLine + pcName + " user GP update failed...";
                }

                if (output.Contains("Aktualizace zásady počítače byla úspěšně dokončena."))
                {
                    result = result + Environment.NewLine + pcName + " computer GP update success...";
                }
                else
                {
                    result = result + Environment.NewLine + pcName + " computer GP update failed...";
                }

                PSEXEC.output = PSEXEC.output + Environment.NewLine + result;
            });
        }

and I call it like this:

private async void gpUpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            List<Task> tasks = new List<Task>();
            foreach (var pc in pcNamesListBox.Items)
            {
                tasks.Add(PSEXEC.gpUpdate(pc.ToString(), lclUsernameTxtBox.Text, lclPasswordTxtBox.Password));
            }

            Task.WaitAll(tasks.ToArray());
            textBlock.Text = PSEXEC.output;
        }

but the UI still freezes and i have not been able to make this work for past 2,5 hours. Could someone look into that please?

您应该使用await Task.WhenAll而不是Task.WaitAll

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