CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

form controll threading topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 10-04-2010, 08:41 PM
Junior Member
 
Join Date: Oct 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Larsonator is on a distinguished road
| More
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

Code:
public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		delegate void OutPutBoxAppend(System::String^ ms);
		OutPutBoxAppend^ myDelegate;
			
		Form1(void)
		{	
			CONNECTED = false;
			connectSockets();
			myDelegate = gcnew OutPutBoxAppend(this, &Form1::OutPutBoxWork);
			Application::Run(gcnew loginForm(commandSocket));
			InitializeComponent();

			Sleep(10);
			Thread^ oThread = gcnew Thread(gcnew ParameterizedThreadStart(Form1::ThreadProc1));
			oThread->Start(this);
			//
			//TODO: Add the constructor code here
			//
		}

		void OutPutBoxWork(System::String^ ms)
		{
			this->ouputBox->AppendText(ms);	
		}

/////////////////////////////////////////////////////////////////////////////////////////////////////--code gap

		static void ThreadProc1(System::Object ^obj)
		{
			Form1^ ob = (Form1^) obj;
			
			for(;; )
			{
				string recvStr;
				if((recvStr = ob->recvData(ob->getMonSocket())) == "")
				{
					MessageBox::Show(L"could not receive stream");
					break;
				}else{
					
					using namespace System::Runtime::InteropServices;
					recvStr.append("\r\n");
					System::String^ ms;
					ms = Marshal::PtrToStringAnsi(static_cast(const_cast(recvStr.c_str())));

					if(ob->ouputBox->InvokeRequired)
					{
						OutPutBoxAppend ^d = gcnew OutPutBoxAppend(^ms);
						ob->Invoke(ob->myDelegate);
					}else{
						ob->ouputBox->AppendText(ms);
					}
				}
			}

		}
Edit: This obviously doesnt build, i was trying to addapt one of the examples givin on the msdn website.

Last edited by shabbir; 10-05-2010 at 12:29 AM. Reason: Code Blocks
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Send data from one Win form to another optimus1 C# Programming 1 11-30-2010 10:21 PM
Using Form text box data aabundle C# Programming 1 11-30-2010 10:18 PM
Dynamic Windows Form building htetnaing C# Programming 2 11-23-2010 02:19 PM
pass an image from form 2 to form1 sjn21682 C# Programming 1 09-08-2010 01:16 AM
Exiting a form and going to the next using a button hamhallan C# Programming 0 04-09-2008 11:18 AM



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.