异步仍然冻结ui

时间:2018-09-26 08:40:16

标签: c# asynchronous async-await

我从没有完全了解异步编程。 我有这种方法:

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;
        }

,但是UI仍然冻结,并且我在过去的2.5小时内都无法进行这项工作。有人可以调查一下吗?

1 个答案:

答案 0 :(得分:3)

您应该使用onRejected而不是class RestaurantEditPage extends React.Component { constructor(props) { super(props); this.state = { restaurantsOpen: false, facilitiesOpen: false, descriptionOpen: false } this.handleClick = this.handleClick.bind(this); } handleClick(section) { if (section === 'restaurant') { this.setState({ restaurantOpen: !this.state.restaurantsOpen }); } if (section === 'facilities') { this.setState({ facilitiesOpen: !this.state.facilitiesOpen }); } if (section === 'description') { this.setState({ descriptionOpen: !this.state.descriptionOpen }); } } render() { return( <RestaurantsCompletionSection ... open=(this.state.restaurantsOpen) clickFunction={() => this.handleClick("restaurants")} /> <FacilitiesSection ... open=(this.state.facilitiesOpen) clickFunction={() => this.handleClick("facilities")} /> <DescriptionSection ... open=(this.state.descriptionOpen) clickFunction={() => this.handleClick("description")} /> )}