Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步仍然冻结用户界面_C#_Asynchronous_Async Await - Fatal编程技术网

C# 异步仍然冻结用户界面

C# 异步仍然冻结用户界面,c#,asynchronous,async-await,C#,Asynchronous,Async Await,我从未完全理解过异步编程。 我有这个方法: internal static Task gpUpdate(string pcName, string localUser, string localPass) { return Task.Run(() => { string result = ""; string command = @"/C psexec \\" +

我从未完全理解过异步编程。 我有这个方法:

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;
            });
        }
我这样称呼它:

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;
        }
private async void gpUpdateBtn\u单击(对象发送方,路由目标)
{
列表任务=新列表();
foreach(pcNamesListBox.Items中的var pc)
{
添加(PSEXEC.gpUpdate(pc.ToString(),lclUsernameTxtBox.Text,lclPasswordTxtBox.Password));
}
Task.WaitAll(tasks.ToArray());
textBlock.Text=PSEXEC.output;
}

但用户界面仍然冻结,在过去的2.5个小时里,我一直无法使其正常工作。有人可以查看一下吗?

您应该使用
等待任务。当所有
而不是
任务。等待所有

按钮单击事件发生在UI线程上。在这里,你告诉线程等待所有任务完成(Task.WaitAll)——这就是你的UI被冻结的原因。你到底在等待什么,眯着眼睛,我在等待所有远程计算机获得组策略更新。我知道这是一件小事,我试图在方法本身中使用wait。谢谢