Adress of thread
When I create a thread, I need to give it a method to be executed. Must this method be in a class? I would like to use a method in my main program (which is Form1). Is this possible?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Per JespersenPosted Jul 25, 2012, 5:08 AM
Control.CheckForIllegalCrossThreadCalls = false;
Which I copied from an old VB program. It works and I hope it doen't give any greif later on, in which case, I'll try your suggestion.
thanks again!
VulpesPosted Jul 25, 2012, 4:52 AM
Notice also that if you're accessing controls (such as textBox1) on a different thread to the one they were created on (the UI thread) then sometimes the code will work and sometimes it won't.
For it to always work, you need to marshal the code which accesses the control back to the UI thread. Here's how you could do it in this particular case :
Per JespersenPosted Jul 25, 2012, 4:38 AM
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Threading.Thread MainThread;
MainThread = new Thread(Method);
MainThread.Start();
void Method()
{
TextBox1.Text = "Written in a thread";
}
}
}
VulpesPosted Jul 25, 2012, 4:00 AM
Form1 is itself just a class (which inherits from Form) so, as long as the method is accessible from where-ever you're creating the thread, there's no reason why you can't use one of its methods for this purpose.