C# - 带有Marquee样式的ProgressBar

dob*_*gab 2 c# progress-bar

我想在带有Marquee样式的表单上添加ProgressBar,以向用户显示正在进行的操作.在耗时的操作期间,表单不会更新,因此ProgressBar也会"冻结".

我查了几篇关于BackgroundWorker的帖子,但在我的情况下,操作不会报告进度,这就是我需要一个Marquee栏的原因.

任何帮助或代码片段都表示赞赏.

注意:我需要使用.NET 4.0(支持XP),所以我不能使用Task.Run :(

button1_Click(object sender, EventArgs e)
{
    progressBar1.Style = ProgressBarStyle.Marquee;
    progressBar1.MarqueeAnimationSpeed = 50;

    // INSERT TIME CONSUMING OPERATIONS HERE
    // THAT DON'T REPORT PROGRESS
    Thread.Sleep(10000);

    progressBar1.MarqueeAnimationSpeed = 0;
    progressBar1.Style = ProgressBarStyle.Blocks;
    progressBar1.Value = progressBar1.Minimum;

}
Run Code Online (Sandbox Code Playgroud)

Idl*_*ind 8

我查了几篇关于BackgroundWorker的帖子,但在我的情况下,操作不会报告进度,这就是我需要一个Marquee栏的原因.

您可以使用BackgroundWorker,只是不要使用它的"进度"部分.这两件事并不相互排斥......

例:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        progressBar1.Style = ProgressBarStyle.Marquee;
        progressBar1.MarqueeAnimationSpeed = 50;

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.RunWorkerCompleted += bw_RunWorkerCompleted;
        bw.RunWorkerAsync();
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        // INSERT TIME CONSUMING OPERATIONS HERE
        // THAT DON'T REPORT PROGRESS
        Thread.Sleep(10000);
    }

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

        button1.Enabled = true;
        MessageBox.Show("Done!");
    }
Run Code Online (Sandbox Code Playgroud)