我创建了一个按钮来启动计时器和计时器的间隔是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