c# – 似乎无法获得动画的进度条

前端之家收集整理的这篇文章主要介绍了c# – 似乎无法获得动画的进度条前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,所以我有一个像这样的问题.
public Class A{

     public A(){
         progressBar.Style = ProgressBarStyle.Marquee;
         progressBar.MarqueeAnimationSpeed = 0;    
     }

     public void DoSomething(){
         if(checkpasses){
             progressBar.MarqueeAnimationSpeed = 100;
             //Do something here...
             progressBar.MarqueeAnimationSpeed = 0;
         }
         else
             //Do nothing...
         }
}

问题是我的进度条根本不会开始移动.首先,我认为它不会自己创建一个新线程(我发现有线)所以我尝试创建一个线程,但仍然是相同的结果.什么都没发生.这是我忘记的事吗?

解决方法

您在此处“执行某些操作”代码将阻止UI线程,因此在DoSomething方法完成之前,您将看不到进度条更新.那时你将动画速度设置回0.

尝试将您的“在这里做一些”代码放在一个单独的线程上.该线程完成后,将动画速度设置回0.

像这样的东西:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
        progressBar1.MarqueeAnimationSpeed = 0;
        progressBar1.Style = ProgressBarStyle.Blocks;
        progressBar1.Value = progressBar1.Minimum;
    }

    private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)
    {
        DoSomething();
    }

    private void button1_Click(object sender,EventArgs e)
    {
        progressBar1.Style = ProgressBarStyle.Marquee;
        progressBar1.MarqueeAnimationSpeed = 100;
        backgroundWorker1.RunWorkerAsync();
    }

    private void DoSomething()
    {
        Thread.Sleep(2000);
    }
}
原文链接:https://www.f2er.com/csharp/98145.html

猜你在找的C#相关文章