Click to See Complete Forum and Search --> : Asynchronous Delegate call problems..


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???

Skizmo
March 3rd, 2008, 08:31 AM
1. Please use [ code ] tags
2. Wrong part of the forum. You are using 'managed' C++. We don't. Try asking your question here (http://www.codeguru.com/forum/forumdisplay.php?f=17)

Marc G
March 3rd, 2008, 12:26 PM
[ moved thread ]

[ removed duplicate thread ]