XpyXt

Hi, i am using C++/CLI 2005 and trying get properties of Form from thread.

In Form.h

#include "thread.h"

#pragma once

namespace pag34 {

...[ ]

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

MyThreadPrue ^MyThr = gcnew MyThreadPrue();

Thread ^Thr2 = gcnew Thread(gcnew ParameterizedThreadStart(MyThr,&MyThreadPrue::VentanaX));

Thr2->Start(this);

}

In Thread.h

#ifndef SysThread_h

#define SysThread_h

ref class MyThreadPrue

{

public:

static void StaticThreadPrueb();

static void VentanaThr();

void VentanaX(Object ^name);

};

#endif

In Thread.cpp

#include "thread.h"

//using namespace System::Windows::Forms;

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace System::Threading;

void MyThreadPrue::StaticThreadPrueb()

{

for (int i = 0; i < 10; i++)

{

MessageBox::Show("Thread GO!");

}

}

void MyThreadPrue::VentanaThr()

{

//Form ^Form1 = gcnew Form;

}

void MyThreadPrue::VentanaX(System::Object ^name)

{

Form ^MiForm = gcnew Form;

MiForm ^ob = (MiForm^) name;

}

---- EOF SOURCE -----

The ob object dont create and give error, and i dont get properties of form1.

Any idea



Re: Windows Forms General Get properties of Form from thread

nobugz

Bunch of problems. You are mixing up variable and type names. You're trying to cast with a variable name. You don't #include "Form1.h". But most of all, you can't create forms in threads, they won't work without a message loop. Create them on your main thread and use Control.Invoke() to access them from a thread. Or use BackgroundWorker.





Re: Windows Forms General Get properties of Form from thread

XpyXt

I dont want create new Form from thread, i am trying change value of label1 in Form1.
If i delete #include "thread.h" of Form1.h i dont call function of thread "StaticThreadPrueb"

Thank





Re: Windows Forms General Get properties of Form from thread

XpyXt

Solution

in thread.cpp

#include "thread.h"

#include "Form1.h"

using namespace pag34;

void MyThreadPrue::VentanaX(System::Object ^name)

{

Form1 ^ob = (Form1^) name;

for( int i = 0; i < 100; i++)

{

Thread::Sleep(100);

ob->label3->Text = i.ToString();

}

ob->label3->Text = L"END";

}