somu0915
March 3rd, 2008, 08:24 AM
Hi, I am very new to C++/CLI thread programming..
I have jst learned that BeginInvoke() and EndInvoke() are two methods for asynchronous thread programming..
So I am trying to build a simple application which has two labels and I am creating two threads, which should change the values of two labels simultaneously..
Here's the code:
public: delegate void InvokeDelegatef1();
private: System::Void button1_Click(System:bject^ sender, System::EventArgs^ e) {
BeginInvoke(gcnew InvokeDelegatef1(this, &Form1::func1));
BeginInvoke(gcnew InvokeDelegatef1(this, &Form1::func1));
}
void func1()
{
for(int a = 0; a < 5; a++)
{
Thread:Sleep(100);
this->label1->Text = Convert::ToString(a);
this->label1->Update();
}
}
void func2()
{
for(int a = 0; a < 25; a++)
{
Thread:Sleep(500);
this->label2->Text = a.ToString();
this->label2->Update();
}
}
However it doesnt update the two labels simultaneously..
It first executes func1() and then func2()..
Why??
Anyway to get around this problem???
I have jst learned that BeginInvoke() and EndInvoke() are two methods for asynchronous thread programming..
So I am trying to build a simple application which has two labels and I am creating two threads, which should change the values of two labels simultaneously..
Here's the code:
public: delegate void InvokeDelegatef1();
private: System::Void button1_Click(System:bject^ sender, System::EventArgs^ e) {
BeginInvoke(gcnew InvokeDelegatef1(this, &Form1::func1));
BeginInvoke(gcnew InvokeDelegatef1(this, &Form1::func1));
}
void func1()
{
for(int a = 0; a < 5; a++)
{
Thread:Sleep(100);
this->label1->Text = Convert::ToString(a);
this->label1->Update();
}
}
void func2()
{
for(int a = 0; a < 25; a++)
{
Thread:Sleep(500);
this->label2->Text = a.ToString();
this->label2->Update();
}
}
However it doesnt update the two labels simultaneously..
It first executes func1() and then func2()..
Why??
Anyway to get around this problem???