C# Winforms进度条工作不正常
这是我第一次使用进度条。我无法在进度条中看到进度指示。我已经编写了以下代码C# Winforms进度条工作不正常,c#,winforms,C#,Winforms,这是我第一次使用进度条。我无法在进度条中看到进度指示。我已经编写了以下代码 using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 1000000;
progressBar1.Value = 0;
progressBar1.Step=10;
Int64 i = 10000000000;
while (i != 1)
{
i = i / 10;
System.Threading.Thread.Sleep(1000);
progressBar1.Increment(10);
}
}
}
}
我的进度栏中没有显示任何进度。请给我一个解决方案(最多尝试100;)
使用/10,您不会运行100000次(1000000次/步骤10)。您将获得10;) 您对进度条的基本使用是正确的,但您的其他一些值有些奇怪。事实上,您的代码将部分工作,但循环将在完成进度条之前完成(我的快速计算表明,即使循环继续,完成填充进度条也需要大约28分钟!) 换句话说,您可能只是没有看到进度条中的更改,因为它太小了 稍加修改可能会稍微改进示例,并显示进度条按预期工作(并且比原始代码快一点)
阻止前台主线程对我来说似乎不是一个好主意。@UweKeim是真的,但我假设Shruti只是想测试用于显示和更新进度条的基本API,所以在这种情况下它可能并不重要。非常感谢您的清晰解释。我感谢你的帮助
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 10; // Smaller number of steps needed
progressBar1.Value = 0;
progressBar1.Step = 1;
Int64 i = 10000000000;
while (i != 1) // This will require 10 iterations
{
i = i / 10;
System.Threading.Thread.Sleep(1000);
progressBar1.Increment(1); // one step at a time
}
}