我有一個簡單的表單,應該啓動一個計時器,執行耗時的操作,並在該操作正在工作時以特定間隔更新進度條。目前,耗時的操作必然會涉及到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