我有一个简单的表单,应该启动一个计时器,执行耗时的操作,并在该操作正在工作时以特定间隔更新进度条。目前,耗时的操作必然会涉及到SearchButton
。然而,没有任何反应进度条,即使耗时的操作(在这种情况下下载),并需要几秒钟:如何设置ProgressBar以在每个计时器的滴答时间递增?
public partial class Form1 : Form
{
System.Windows.Forms.Timer searchProgressTimer;
public Form1()
{
InitializeComponent();
this.searchProgressTimer = new System.Windows.Forms.Timer();
}
private void InitializeTimer()
{
this.searchProgressTimer.Interval = 250;
this.searchProgressTimer.Tick += new EventHandler(searchProgressTimer_Tick);
}
void searchProgressTimer_Tick(object sender, EventArgs e)
{
searchProgressBar.Increment(1);
if (searchProgressBar.Value == searchProgressBar.Maximum)
this.searchProgressTimer.Stop();
}
private void SearchDatabase_Click(object sender, EventArgs e)
{
this.searchProgressTimer.Start();
// Time-consuming operation
String filename = @"http://www.bankofengland.co.uk/publications/Documents/quarterlybulletin/qb0704.pdf";
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(filename), @"file.pdf");
int test;
for (int i = 0; i < 100000; i++)
for (int j = 0; j < 100000; j++)
test = i + j;
this.searchProgressTimer.Stop();
}
}
(该功能被命名为一点点奇怪,因为实际的耗时的操作是一个数据库搜索,但该代码,虽然正常工作,是非常漫长,并涉及)。
调试此代码只是向我显示SearchButton_Click
事件处理程序正确触发,但代码从不跳转到事件处理程序。有任何想法吗?
我看不到任何InitializeTimer()的调用 – 2012-07-13 18:41:32
把断点'searchProgressBar.Incriment(1)'。您可以通过单击该行左侧的边距来完成此操作。一个红点会出现让你知道一个断点在那里。在调试器中运行该程序,查看断点是否被命中。 – 2012-07-13 18:43:31
http://stackoverflow.com/a/11033200/763026 – 2012-07-14 14:31:48