简体   繁体   English

c#进度条IsIndeterminate动画卡在GUI线程中

[英]c# Progress Bar IsIndeterminate animation stuck in GUI thread

I got this code below that is called whenever a button is clicked. 我得到下面的这段代码,每当单击一个按钮时就会调用它。 Now, since it's using Telnet and I had to add a bunch of Thread.Sleep(), this method takes a good 5-7 seconds to execute. 现在,由于它使用的是Telnet,而且我必须添加一堆Thread.Sleep(),因此此方法需要5-7秒钟才能执行。

I was wondering if theres a way to have the animation of the Progress Bar at the same time as this run to let the user know that everything is fine. 我想知道是否有一种方法可以让进度条同时运行,以使用户知道一切都很好。

Thanks. 谢谢。

private void rack_Click(object sender, RoutedEventArgs e)
            {
                progressBar.IsIndeterminate = true;

                // Extrait le nom du rack sélectionné 
                selectedRack = (sender as Button).Content.ToString().ToLower();

                // Console Update
                console.Text += "Connexion en cours...";

                // Ouvre la connection au PDU
                clientPDU = this.getConnectionPDU();

                // S'assure que la connection fut éffectué correctement
                if (clientPDU != null)
                {
                    // Disable les autres racks
                    for (int i = 65; i < 71; i++)
                    {
                        Button btn = (Button)FindName("rack_" + ((char)i));
                        if (btn.Content.ToString().ToLower() != selectedRack)
                            btn.IsEnabled = false;
                    }

                    // Active Rafraichir et Déconnecter
                    refresh.IsEnabled = true;
                    disconnect.IsEnabled = true;

                    // Console Update
                    console.Text += "\nConnexion établie";

                    // Mettre à jour les Port qui sont ouvert/fermé
                    this.getPortStatus();

                    // Active les boutons en correspondance à ce qui est On et Off
                    this.setButtonStatus();
                }
                else
                {
                    // Console Update
                    console.Text += "\n\nConnexion Impossible. Veuillez réessayer...\n";
                }

                // Scroll to end
                scroller.ScrollToEnd();

                progressBar.IsIndeterminate = false;
            }

You will need to set the IsIndeterminate property to false and specify values for the progress bar throughout the operation in order to provide for animation of the progressbar.: 您需要将IsIndeterminate属性设置为false,并在整个操作过程中为进度条指定值,以便提供进度条的动画。

progressBar.IsIndeterminate = false;
// set the maximum for the progressBar
progressBar.Maximum = 100;
// do work and increment value
progressBar.Value++;

The following page has an example of a ProgressBar with the IsIndeterminate property set to true and a picture of what the animation looks like (it provides no real user feedback). 下一页包含一个ProgressBar的示例,该示例的IsIndeterminate属性设置为true,并且显示了动画的外观(不提供实际的用户反馈)。 The page also has an example with IsIndeterminate set to false, but values must be specified. 该页面上还有一个示例,其中IsIndeterminate设置为false,但是必须指定值。 In this example, the work is done on a separate thread so as to allow for UI interaction while the work is being performed. 在此示例中,工作在单独的线程上完成,以便在执行工作时允许UI交互。

http://www.wpf-tutorial.com/misc-controls/the-progressbar-control/ http://www.wpf-tutorial.com/misc-controls/the-progressbar-control/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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