I could use some help with the following issue:
I have a Form which has a public TextBox member... that form loads up a new thread which is defined in a separate class.
The thread has a constant loop in it.. and I need to update the textbox on my form with messages obtained at the end of each cycle in that loop.. in C++ that would be easy with just passing a few pointers when I create the thread class.. but in C# I have to enagle unmanaged code and still I can't really pass the textbox as a pointer... so I'm looking for some other solution if there is one.. (tried messing with the ActiveForm and such but can't really get it going)
Any quick help would be awsome.. :P
Thanks
Rasmus StriibPosted Jul 4, 2010, 3:43 PM
Note that I have simply written the code here in the editor - but it should compile.
namespace MyNamespace
{
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.TextBox m_log;
Worker workerObject;
Thread workerThread;
private void startthread()
{
workerObject = new Worker(m_log);
workerThread = new Thread(new ThreadStart(workerObject.DoWork));
workerThread.Start();
}
}
public class Worker
{
private TextBox textBox;
public Worker(Textbox textBox)
{
this.textBox = textBox;
}
public void DoWork()
{
while (!_shouldStop)
{
//I need to set the value of the Text property for my m_log TextBox from the other class and I don't have an instance of that class here...
SetText("Hello World!");
}
}
private delegate void SetTextDelegate(string text);
private void SetText(string text)
{
if (textbox.InvokeRequired)
{
SetTextDelegate d = new SetTextDelegate(SetText);
textBox.Invoke(d, new object[] { text });
}
else
{
textBox.Text = text;
}
}
}
}
SimonPosted Jul 4, 2010, 8:58 PM
Thanks for the help
Cheers
Deepak BhatiaPosted Jul 4, 2010, 9:41 AM
There were light problems here due to heavy rainfall. Just wait a bit I will post an working example on how to do it.
This would help you out.
SimonPosted Jul 4, 2010, 8:07 AM
I forgot to mention that I'm using .net 1.1 :D
That one has some form of delegates aswell though I don't quite get how they work
My application's structure looks like this, I'll leave out the usual stuff so this is just a sketch:
namespace MyNamespace
{
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.TextBox m_log;
Worker workerObject;
Thread workerThread;
private void startthread()
{
workerObject = new Worker();
workerThread = new Thread(new ThreadStart(workerObject.DoWork));
workerThread.Start();
}
}
public class Worker
{
public void DoWork()
{
while (!_shouldStop)
{
//I need to set the value of the Text property for my m_log TextBox from the other class and I don't have an instance of that class here...
}
}
}
}
My question is.. how do I do this using delegates?
Thanks for the quick reply
Deepak BhatiaPosted Jul 4, 2010, 7:44 AM
In C# it is possible to use cross thread functionalities using events and delegates.
Just see simple example
http://www.akadia.com/services/dotnet_delegates_and_events.html
http://www.codeproject.com/KB/cs/events.aspx
If you have problem just reply, I will help you out.