October 4th, 2010, 09:21 PM
form controll threading
I realise this topic has been covered before,
but i am struggling to grasp the concept of delegates and stuff.
my program has a multi lines textbox, and a function (which runs on a different thread) that runs in a loop continuesly receiving data from a socket. this data comes in the form of a string, and it is made into a system.string, and is then appended to the textbox
the problem i am having is that the textbox is created on thread1, where has the function is on thread2. trying to access the textbox from thread 2 makes baby jesus cry. I understand that this is where i need to use the invoke methods, and delegate functions, but as to implimenting these processing to solve my delema is still beyond my grasp.
i can show you what i have so far in terms of relevant code
public ref class Form1 : public System::Windows::Forms::Form
delegate void OutPutBoxAppend(System::String^ ms);
CONNECTED = false;
myDelegate = gcnew OutPutBoxAppend(this, &Form1::OutPutBoxWork);
Thread^ oThread = gcnew Thread(gcnew ParameterizedThreadStart(Form1::ThreadProc1));
//TODO: Add the constructor code here
void OutPutBoxWork(System::String^ ms)
static void ThreadProc1(System::Object ^obj)
Form1^ ob = (Form1^) obj;
if((recvStr = ob->recvData(ob->getMonSocket())) == "")
MessageBox::Show(L"could not receive stream");
using namespace System::Runtime::InteropServices;
ms = Marshal::PtrToStringAnsi(static_cast<IntPtr>(const_cast<char *>(recvStr.c_str())));
OutPutBoxAppend ^d = gcnew OutPutBoxAppend(^ms);
Edit: This obviously doesnt build, i was trying to addapt one of the examples givin on the msdn website.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.