Hi all;
I'd want to know how to access & modify parameters of a form by a thread. In fact, I want to change the text of a text box, or a value of a progress bar of the form1 in my thread.
I tried this but without success, I pass the form in argument to the thread
namespace
Essai{
static class Program{
/// <summary> /// The main entry point for the application. /// </summary>[
MTAThread] static void Main(){
Application.Run(new Form1());}
}
}
public partial class Form1{
System.ComponentModel.IContainer components = null;System.Windows.Forms.
MainMenu mainMenu1; /// Clean up any resources being used. protected override void Dispose(bool disposing){...
} void InitializeComponent(){ . . .}
System.Windows.Forms.
MenuItem menuItem1;System.Windows.Forms.
MenuItem menuItem2; public System.Windows.Forms.TextBox textBox1;System.Windows.Forms.
Timer timer1;}
public
partial class Form1 : Form{
Thread t; EnvoiPhotos Envoi; public Form1(){
InitializeComponent();
}
void menuItem2_Click(object sender, EventArgs e){
t.Abort();
Application.Exit();}
void timer1_Tick(object sender, EventArgs e){
timer1.Enabled =
false; try{
Envoi =
new EnvoiPhotos(this);t =
new Thread(new ThreadStart(Envoi.debut));t.Start();
}
catch (Exception ex){
MessageBox.Show(ex.Message);}
}
public class EnvoiPhotos{
Form1 fenetre; public EnvoiPhotos(Form1 fenetre){
this.fenetre = fenetre;}
public void debut(){
try{
fenetre.textBox1.Text =
"Show this text"; } catch (Exception ex){
MessageBox.Show(ex.Message);}
}
}

Modify a form by a thread
Patrick Berny
Try this
public class Form1 : Form
{
delegate void MuiltiThreadedDelegate();
MuiltiThreadedDelegate multiThreaded;
public Form1()
{
multiThreaded=new MuiltiThreadedDelegate(DelegateMethod);
InitializeComponent();
}
void DelegateMethod()
{
Form1.TextBox1="Some Msg";
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t= new Thread(Status);
t.Start();
}
private void Status()
{
this.Invoke(DelegateMethod);
}
}
Hope it gives you an idea about how it works..
Rob Boisjolie
Hi,
I will recommend you to follow some pattern, threads are normally complex to handle.
I do not have complete knowledge of your application scope but as per my understanding you have a main form that will execute other forms in thread and you want to be notified at main form that some threaded from has done some thing that could be any thing text change etc. I think that you should use the Observer pattern so that your main form could observer the treaded forms.
Here! you can find some good patterns including observer pattern and there implementation in C#.
Hope this help.
sharc
In fact, like in a default project for smart device of VS2005, the main fonction call a new form1().In this form, I want to launch a thread which will be able to change text and value of the form.
Uwe Helmer
Thanks all for your answers!
I succedded to do this job with delegate and Invoke methods. It's a bit difficult!
Emanuele Ornella
Hi,
I think I am clear on your problem. You can simply create a static function on you Form1.
public static void TellMeSomeThing(string msg)
{
MessageBox.Show("Threaded form told that"+msg);
}
In your 2nd form which will be executed using thread when ever you want to tell some thing to main form just call this method like this
Form1.TellMeSomeThing("some message or textbox1.text etc");
If you have many threaded forms accessing this method then you can also use following for safty…
lock (this)
{
Form1.TellMeSomeThing("some message or textbox1.text etc");
}
Now you need not to pass your Form1 as a parameter as well…
Hope this help
john k
dd_helper
From the sounds of it you’ve run into that wonderful little feature where a thread you’ve created cannot modify controls that are on a form that is running in a different thread.
In order to get around this, you need to have the control owners thread actually do the work for you.
The common way to do this is to have your thread call a function which calls Invoke on the object it needs to access (or the object owning it) and passes in a delegate to the function it wants called in the ui thread (along with any arguments).
While a little verbose, take a look at this CodeProject article on threading which covers this topic pretty well.