进度条绘制太慢 [英] Progressbar draws too slowly

查看:80
本文介绍了进度条绘制太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小对话框类,我可以在漫长的过程中弹出它并设置标题,消息和进度条值。 此对话框自动计算完成百分比,并将其显示在进度条下的标签中。 

我的问题是,我的大多数进程都会比对话框中的进度条更快地增加进度对话框中的值,并且可以显示更改。 这最终导致带有我计算的百分比的标签在进度条显示100%之前达到100%。在某些情况下,进度条看起来像是在50%或更低。

有没有办法通过在我的线程中休眠直到进度条完成更新来同步进度条? 下面是进度条对话框布局和代码的副本。


部分类frmProgress 
{
///< ;总结>
///所需的设计变量。
///< / summary>
private System.ComponentModel.IContainer components = null;

///< summary>
///清理正在使用的所有资源。
///< / summary>
///< param name =" disposing">如果应处理托管资源,则为true;否则,假。< / param>
protected override void Dispose(bool disposing)
{
if(disposing&&(components!= null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows窗体设计器生成的代码

///< summary>
/// Designer支持的必需方法 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.pbar = new System.Windows.Forms.ProgressBar();
this.lblPercent = new System.Windows.Forms.Label();
this.lblMessage = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pbar
//
this.pbar.Location = new System.Drawing.Point(12,39);
this.pbar.Name =" pbar" ;;
this.pbar.Size = new System.Drawing.Size(260,23);
this.pbar.Step = 1;
this.pbar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
this.pbar.TabIndex = 0;
//
// lblPercent
//
this.lblPercent.Location = new System.Drawing.Point(12,65);
this.lblPercent.Name =" lblPercent" ;;
this.lblPercent.Size = new System.Drawing.Size(260,23);
this.lblPercent.TabIndex = 1;
this.lblPercent.Text =" 0%" ;;
this.lblPercent.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// lblMessage
//
this.lblMessage.Font = new System.Drawing.Font(" Microsoft Sans Serif",10F,System.Drawing.FontStyle .Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));
this.lblMessage.ForeColor = System.Drawing.Color.Navy;
this.lblMessage.Location = new System.Drawing.Point(12,9);
this.lblMessage.Name =" lblMessage" ;;
this.lblMessage.Size = new System.Drawing.Size(260,27);
this.lblMessage.TabIndex = 2;
this.lblMessage.Text =" Message";
this.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// frmProgress
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284,96);
this.ControlBox = false;
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.lblPercent);
this.Controls.Add(this.pbar);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name =" frmProgress" ;;
this.Text =" Please Wait ..." ;;
this.Load + = new System.EventHandler(this.frmProgress_Load);
this.FormClosed + = new System.Windows.Forms.FormClosedEventHandler(this.frmProgress_FormClosed);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ProgressBar pbar;
private System.Windows.Forms.Label lblPercent;
private System.Windows.Forms.Label lblMessage;
}

使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

公共部分类frmProgress:表格
{
public void Increment(int Value)
{
pbar.Increment(Value);
Recalc();
}
public void PerformStep()
{
pbar.PerformStep();
Recalc();
}


public int总计
{
get
{
return pbar.Maximum;
}

设置
{

pbar.Maximum = value;
pbar.Update();
Application.DoEvents();
Recalc();
}
}

公共字符串ProgressMessage
{
get
{
return lblMessage.Text;
}


set
{
lblMessage.Text = value;
}

}

public int Value
{
get
{
return pbar.Value;
}

set
{
pbar.Value = value;
Recalc();
}

}
public frmProgress()
{
InitializeComponent();
}

private void frmProgress_Load(object sender,EventArgs e)
{

Cursor.Current = Cursors.WaitCursor;
}

private void Recalc()
{
if(pbar.Maximum == 0)
{
lblPercent.Text =" ; 0%英寸;
}
else
{

lblPercent.Text =((float)pbar.Value /(float)pbar.Maximum).ToString(" 0.# %QUOT);
}
Refresh();
}

private void frmProgress_FormClosed(object sender,FormClosedEventArgs e)
{
Cursor.Current = Cursors.Default;
}




}





谢谢,
John Hamilton
POSitive Software Company
http://www.gopositive.com

解决方案

在这个帖子中查看我的表格,看看你的表格。

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/52dcb460-90a3-45f2-ae23-bcb60514c542

它在VB,C#和C ++中具有相同的形式。

鲁迪  = 8 ^ d

I have written a little dialog class that I can pop up during long processes and set the Title, message, and progress bar values.  This dialog automatically calculates the percentage of completion and displays it in a label under the progress bar. 

My problem is that most of my processes increment the value in the progress dialog quicker than the progressbar on the dialog can draw and display the changes.  This ends up causing the label with my calculated percentage to reach 100% WAY before the progress bar show 100%. The progress bar in some cases looks like it is at 50% or less.

Is there a way to syncronize the progressbar by sleeping my thread until the progress bar is done updating?  Below is a copy of the progress bar dialog Layout and Code.

    partial class frmProgress
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pbar = new System.Windows.Forms.ProgressBar();
            this.lblPercent = new System.Windows.Forms.Label();
            this.lblMessage = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // pbar
            // 
            this.pbar.Location = new System.Drawing.Point(12, 39);
            this.pbar.Name = "pbar";
            this.pbar.Size = new System.Drawing.Size(260, 23);
            this.pbar.Step = 1;
            this.pbar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            this.pbar.TabIndex = 0;            
            // 
            // lblPercent
            // 
            this.lblPercent.Location = new System.Drawing.Point(12, 65);
            this.lblPercent.Name = "lblPercent";
            this.lblPercent.Size = new System.Drawing.Size(260, 23);
            this.lblPercent.TabIndex = 1;
            this.lblPercent.Text = "0%";
            this.lblPercent.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // lblMessage
            // 
            this.lblMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblMessage.ForeColor = System.Drawing.Color.Navy;
            this.lblMessage.Location = new System.Drawing.Point(12, 9);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(260, 27);
            this.lblMessage.TabIndex = 2;
            this.lblMessage.Text = "Message";
            this.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frmProgress
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 96);
            this.ControlBox = false;
            this.Controls.Add(this.lblMessage);
            this.Controls.Add(this.lblPercent);
            this.Controls.Add(this.pbar);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmProgress";
            this.Text = "Please Wait...";
            this.Load += new System.EventHandler(this.frmProgress_Load);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.frmProgress_FormClosed);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ProgressBar pbar;
        private System.Windows.Forms.Label lblPercent;
        private System.Windows.Forms.Label lblMessage;
    }

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    public partial class frmProgress : Form
    {
        public void Increment(int Value)
        {
            pbar.Increment(Value);
            Recalc();
        }
        public void PerformStep()
        {
            pbar.PerformStep();
            Recalc();
        }


        public int Total
        {
            get
            {
                return pbar.Maximum;
            }

            set
            {
                
                pbar.Maximum = value;
                pbar.Update();
                Application.DoEvents();
                Recalc();                
            }
        }

        public string ProgressMessage
        {
            get
            {
                return lblMessage.Text;
            }

            
            set
            {
                lblMessage.Text = value;
            }
            
        }

        public int Value
        {
            get
            {
                return pbar.Value;
            }

            set
            {
                pbar.Value = value;                
                Recalc();
            }

        }
        public frmProgress()
        {
            InitializeComponent();
        }

        private void frmProgress_Load(object sender, EventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
        }

        private void Recalc()
        {
            if (pbar.Maximum == 0)
            {
                lblPercent.Text = "0%";
            }
            else
            {

                lblPercent.Text = ((float)pbar.Value / (float)pbar.Maximum).ToString("0.#%");                
            }
            Refresh();
        }

        private void frmProgress_FormClosed(object sender, FormClosedEventArgs e)
        {
            Cursor.Current = Cursors.Default;
        }




    }




Thanks, John Hamilton POSitive Software Company http://www.gopositive.com

解决方案

Take a look at my Form in this thread, while I take a look at yours.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/52dcb460-90a3-45f2-ae23-bcb60514c542

It has the same form in VB, C#, and C++.

Rudy  =8^D


这篇关于进度条绘制太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆