Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NETC++;计时器勾选功能不工作_.net_Timer_Managed C++ - Fatal编程技术网

.NETC++;计时器勾选功能不工作

.NETC++;计时器勾选功能不工作,.net,timer,managed-c++,.net,Timer,Managed C++,我创建了一个按钮来启动计时器,计时器的间隔是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

我创建了一个按钮来启动计时器,计时器的间隔是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
}
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;
}
编辑:

好的,我会尽量把我的问题解释清楚。 我有一个主窗体,在状态栏中有一个
toolstripprogressbar

当我单击一个按钮时,一个函数将开始解析一个文件,并且进度条必须显示函数的进度。这是我的代码:

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
}
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;
}

当您在计时器事件中使用
this->textBox->Text=“njmk”
时,主线程应该更新textBox文本;但是你让主线程进入睡眠状态,所以不能免费更新文本框
请记住,UI对象是从主线程更新的

如果我们必须运行长过程,让窗口重新绘制并响应用户,这就是我们使用多线程的原因。

它不起作用,因为您正在使线程(UI线程)睡眠1000*1000毫秒(~16分钟):

void按钮1\u单击(…)
{
这个->定时器->开始();
对于(int i=0;i<1000;i++)
线程::睡眠(1000);
此->计时器->停止();
}

这就是它无法更新文本框内容的原因。

似乎正是我已经回答的;)但是是的,你是对的,这就是问题好的,你是对的。那么,当一个函数运行时,我如何获得时间呢?我的意思是,我调用一个函数,假设它需要1分钟,我想每10秒执行一个函数。我该怎么做?@user983924不清楚你想做什么。请重新措辞,然后将其添加到您的问题中。@Naceledine:谢谢,我很高兴,但我没有任何问题:D@user983924:如果要执行多个耗时的函数,请在单独的线程中执行它们(而不是在主线程中)。如果需要同步它们,可以使用互斥体、信号量和.net提供的所有对象。更新progressbar不需要计时器:您需要在
ParseFunction()
内部执行。注意:在调试中,您可能会出现一些错误,因为您试图从另一个线程更新为UI对象。如果直接运行EXE,则不会引发此错误;要在调试期间解决此问题,请使用
if。。。调用所需的…