我的应用程序运行文件,每个文件都有自己的运行时间。此函数以毫秒为单位获取进度时间应运行的时间:
timerProgress = my timer
pbStatus = my progress bar
public void AnimateProgBar(int milliSeconds)
{
if (!timerProgress.Enabled && milliSeconds != 0)
{
pbStatus.Value = 0;;
timerProgress.Interval = milliSeconds / 100;
timerProgress.Enabled = true;
}
}这是我的计时器,它填满了进度条:
private void timerProgress_Tick(object sender, EventArgs e)
{
if (pbStatus.Value < 100)
{
pbStatus.Value += 1;
pbStatus.Refresh();
}
else
{
timerProgress.Enabled = false;
}
}我的问题是进度条运行得太快了,例如,如果AnimateProgBar的值为12000 (12秒),进度条只运行6-7秒。
发布于 2013-04-21 00:02:44
你的代码不能工作,这很奇怪。我试了几次,每次都错过了0.6秒;看起来计时器只是不精确的。
你可以做的是自己管理好时间,而不是相信计时器:
WithEvents Tmr As New Timer With {.Interval = 100}
Dim startTime As Date, AnimationTime%
Sub AnimateProgress(ms%)
If ms <= 0 Then Exit Sub
ProgressBar1.Value = 0
AnimationTime = ms
startTime = Now
Tmr.Start()
End Sub
Private Sub Tmr_Tick() Handles Tmr.Tick
ProgressBar1.Value = Math.Min((Now - startTime).TotalMilliseconds / AnimationTime, 1) * 100
If ProgressBar1.Value = 100 Then Tmr.Stop()
End Sub编辑-对回复的响应如下:
哦,对不起,不是,是vb.net。我对这两种语言都很了解,但我更喜欢vb,并且倾向于认为其他语言也是如此。
以下是c#版本:
DateTime startTime; int animationTime;
void AnimateProgress(int ms) {
if (ms <= 0) return;
progressBar1.Value = 0;
animationTime = ms;
startTime = DateTime.Now;
Tmr.Start();
}
private void Tmr_Tick(object sender, EventArgs e) {
progressBar1.Value = (int)(Math.Min((DateTime.Now - startTime).TotalMilliseconds / animationTime, 1) * 100);
if (progressBar1.Value == 100) Tmr.Stop();
}发布于 2013-04-20 23:30:22
您可以尝试使用这个基于PerformStep method的示例
var progressBar = new System.Windows.Forms.ProgressBar();
progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Step = 10;
//begin loop
//Your treatment of step
progressBar.PerformStep();
//end loop网址:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.progressbar.performstep(v=vs.80).aspx
你这里有样品
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;
// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}链接:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.progressbar.performstep(v=vs.80).aspx
发布于 2013-04-21 20:09:58
我不能复制你的问题。我刚刚测试了一个新的表单,带有一个ProgressBar和一个计时器,正如您在问题中详细描述的那样,并且只添加了一个按钮来启动测试,以及一个标签来显示经过的时间:
DateTime start;
private void button1_Click(object sender, EventArgs e)
{
start = DateTime.Now;
AnimateProgBar(12000);
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.Subtract(start).TotalSeconds.ToString();
//the rest of your code, starting with "if (pbStatus.Value < 100)"我一直有12.6秒的时间,直到progressBar填满(计时器停止,冻结标签文本)……也许你的progressBar的一部分被隐藏了?
如果你对BlackCap还注意到的0.6秒的额外时间感到好奇,那是因为你将计时器间隔设置为120毫秒,但计时器事件的分辨率约为18毫秒,因此它实际上将以126秒而不是120秒的速度触发。
https://stackoverflow.com/questions/16121839
复制相似问题