Click to See Complete Forum and Search --> : Simple Threading Problem..


somu0915
March 3rd, 2008, 02:53 AM
I am new to threading concepts in C++/CLI.

I have a button and a label in the form and here is some code for that:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form1^ f = gcnew Form1;
Thread^ myThread = gcnew Thread(gcnew ThreadStart(f, &Form1::test));
myThread->Start();
}


void test()
{
this->label1->Text = "Hi";
this->label1->Update();
}


But this code dosent update the Label.. WHY???
I have also used [MTAThreadAttribute] in my main file as:

[MTAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}


Somebody please help..

darwen
March 3rd, 2008, 03:15 PM
Firstly, don't use MTAThreadAttribute in projects with UI. Otherwise you'll find the common dialogs won't work (e.g. OpenFileDialog etc).

Secondly you can't directly set a window's text from anything other than the thread it was created on. Or perform any other window function for that matter.

Google for InvokeRequired or have a look here ('http://weblogs.asp.net/justin_rogers/pages/126345.aspx') for how to call windows methods from other threads.

Darwen.

somu0915
March 4th, 2008, 02:50 AM
Firstly, don't use MTAThreadAttribute in projects with UI. Otherwise you'll find the common dialogs won't work (e.g. OpenFileDialog etc).

Secondly you can't directly set a window's text from anything other than the thread it was created on. Or perform any other window function for that matter.

Google for InvokeRequired or have a look here ('http://weblogs.asp.net/justin_rogers/pages/126345.aspx') for how to call windows methods from other threads.

Darwen.

hi Darrwen,
Thanx for the help..
But I am not getting the hang of things here..
But could you write a sample code for this??
Thanx..