Have everyone tried to create controls in separated threads ? I have a
problem that I do not understand.
To simplify the explanations, I wrote theses few lines to show an example of
the problem.
This program do nothing more than showing a button "Start thread". When it
is pushed, a new thread is started in which a usercontrol is created,
showing another button. That's all.
Everything works, but, when like this source code I put the line (line 23)
userControl1.Us elessFunction() ;
it fails (lock for a very very long time) and I have to kill the task.
This useless function only shows the button ! That 's strange, isn't it ?
I tried to move the line after the line 24
Invoke(new AddControl_Dele gate(AddControl ),new object[1] {userControl1}) ;
and in this case, it works perfectly !
Could someone help me please ?
Thanks,
Ludovic SOEUR.
Here is the source code :
using System;
using System.Drawing;
using System.Windows. Forms;
using System.Threadin g;
public class Form1 : Form {
[STAThread]
static void Main() {
Application.Run (new Form1());
}
public Form1() {
Button button=new Button();
button.Text = "Start thread";
button.Click += new System.EventHan dler(button_Cli ck);
Controls.Add(bu tton);
}
private void button_Click(ob ject sender, System.EventArg s e) {
(new Thread(new ThreadStart(Sta rtThread))).Sta rt();
}
private void StartThread() {
UserControl1 userControl1=ne w UserControl1();
userControl1.To p=24;
userControl1.Us elessFunction() ;
Invoke(new AddControl_Dele gate(AddControl ),new object[1] {userControl1}) ;
}
private delegate void AddControl_Dele gate(UserContro l1 control);
private void AddControl(User Control1 control) {
Controls.Add(co ntrol);
}
}
public class UserControl1 : UserControl {
private Button button=new Button();
public UserControl1() {
button.Text = "Success";
Controls.Add(bu tton);
}
public void UselessFunction () {
button.Visible= false;
button.Visible= true;
}
}
problem that I do not understand.
To simplify the explanations, I wrote theses few lines to show an example of
the problem.
This program do nothing more than showing a button "Start thread". When it
is pushed, a new thread is started in which a usercontrol is created,
showing another button. That's all.
Everything works, but, when like this source code I put the line (line 23)
userControl1.Us elessFunction() ;
it fails (lock for a very very long time) and I have to kill the task.
This useless function only shows the button ! That 's strange, isn't it ?
I tried to move the line after the line 24
Invoke(new AddControl_Dele gate(AddControl ),new object[1] {userControl1}) ;
and in this case, it works perfectly !
Could someone help me please ?
Thanks,
Ludovic SOEUR.
Here is the source code :
using System;
using System.Drawing;
using System.Windows. Forms;
using System.Threadin g;
public class Form1 : Form {
[STAThread]
static void Main() {
Application.Run (new Form1());
}
public Form1() {
Button button=new Button();
button.Text = "Start thread";
button.Click += new System.EventHan dler(button_Cli ck);
Controls.Add(bu tton);
}
private void button_Click(ob ject sender, System.EventArg s e) {
(new Thread(new ThreadStart(Sta rtThread))).Sta rt();
}
private void StartThread() {
UserControl1 userControl1=ne w UserControl1();
userControl1.To p=24;
userControl1.Us elessFunction() ;
Invoke(new AddControl_Dele gate(AddControl ),new object[1] {userControl1}) ;
}
private delegate void AddControl_Dele gate(UserContro l1 control);
private void AddControl(User Control1 control) {
Controls.Add(co ntrol);
}
}
public class UserControl1 : UserControl {
private Button button=new Button();
public UserControl1() {
button.Text = "Success";
Controls.Add(bu tton);
}
public void UselessFunction () {
button.Visible= false;
button.Visible= true;
}
}
Comment