我創建了一個按鈕來啓動計時器和計時器的間隔是1000.我添加了一個timer_Tick()事件處理程序,但它不工作,我不明白原因。.net C++ timer_tick函數不起作用
這裏是我的代碼:
void button1_Click(...)
{
this->timer->Start();
for(int i = 0; i < 1000; i++)
Thread::Sleep(1000);
this->timer->Stop();
}
void timer_Tick(...)
{
this->textBox->Text = "njmk"; // only to handle while debugging but it is not handled
}
注:我添加了這個:
this->timer->Tick += gcnew System::EventHandler(this, &Form1::timer_Tick);
編輯:
OK,我會盡量解釋清楚我的問題。 我有一個主窗體,並在狀態欄中我有一個toolstripprogressbar
。
當我點擊一個按鈕時,一個函數將開始解析一個文件,並且進度條必須顯示該函數的進度。因此,這裏是我的代碼:
void button_click(...)
{
this->progressBar->Visible = true;
this->backGroundWorker->RunWorkerAsync();
}
void backGorundWorker_DoWork(...)
{
this->timer->Start();
ParseFunction(); // it takes about two minute
this->timer->Stop();
}
void timer_Tick(...)
{
this->bacGroundWorker->ReportProgress(5);
}
void backGroundWorker_ProgressChanged(...)
{
this->progressBar->Value += e->ProgressPercentage();
}
void backGroundWorker_RunWorkerComplete(...)
{
this->progressBar->Visible = false;
}
你不需要計時器來更新進度條:你需要在你的'ParseFunction()'裏面做。注意:在調試中,您可能會遇到一些錯誤,因爲您嘗試從另一個線程更新爲UI對象。如果直接運行EXE,則不會引發此錯誤;在調試過程中使用'if ... InvokeRequired ...'來解決它。 – Marco