.net c++ timer_tick 函数不起作用
我创建了一个按钮来启动计时器,计时器的间隔为1000。 我添加了一个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);
编辑:
好的,我会尽力清楚地解释我的问题。 我有一个主窗体,在状态条中有一个 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;
}
I created a button to start the timer and the interval of timer is 1000.
I added a timer_Tick() event handler but it is not working, I dont understand the reason.
Here is my code:
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
}
Note: I added this:
this->timer->Tick += gcnew System::EventHandler(this, &Form1::timer_Tick);
EDIT :
OK, I will try to explain my problem clearly.
I have a main form and in the status strip I have a toolstripprogressbar
.
When I click a button, a function will start parsing a file and the progress bar must show the progress of the function. So here is my code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在计时器事件中使用
this->textBox->Text = "njmk"
时,主线程应该更新文本框文本;但你正在让主线程休眠,所以更新文本框不是免费的!!请记住,UI 对象是从主线程更新的!
如果我们必须运行长程序并让我们的窗口重新绘制并响应用户,这就是我们使用多线程的原因。
When you use
this->textBox->Text = "njmk"
in timer event, main thread should update textbox text; but you're making main thread to sleep, so it's not free to update textbox!!Remember that UI objects are updated from main thread!
This is the reason for which we use multithread if we have to run long procedures and let our window to be redrawn and respond to user.
它不起作用,因为您使线程(UI 线程)休眠 1000 * 1000 毫秒(约 16 分钟):
这就是它无法更新文本框内容的原因。
It's not working because you're making the thread (UI thread) sleep for 1000 * 1000 milliseconds (~16 minutes):
That's why it can't update the textbox content.