L
Ludovic SOEUR
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.UselessFunction();
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_Delegate(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.Threading;
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.EventHandler(button_Click);
Controls.Add(button);
}
private void button_Click(object sender, System.EventArgs e) {
(new Thread(new ThreadStart(StartThread))).Start();
}
private void StartThread() {
UserControl1 userControl1=new UserControl1();
userControl1.Top=24;
userControl1.UselessFunction();
Invoke(new AddControl_Delegate(AddControl),new object[1] {userControl1});
}
private delegate void AddControl_Delegate(UserControl1 control);
private void AddControl(UserControl1 control) {
Controls.Add(control);
}
}
public class UserControl1 : UserControl {
private Button button=new Button();
public UserControl1() {
button.Text = "Success";
Controls.Add(button);
}
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.UselessFunction();
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_Delegate(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.Threading;
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.EventHandler(button_Click);
Controls.Add(button);
}
private void button_Click(object sender, System.EventArgs e) {
(new Thread(new ThreadStart(StartThread))).Start();
}
private void StartThread() {
UserControl1 userControl1=new UserControl1();
userControl1.Top=24;
userControl1.UselessFunction();
Invoke(new AddControl_Delegate(AddControl),new object[1] {userControl1});
}
private delegate void AddControl_Delegate(UserControl1 control);
private void AddControl(UserControl1 control) {
Controls.Add(control);
}
}
public class UserControl1 : UserControl {
private Button button=new Button();
public UserControl1() {
button.Text = "Success";
Controls.Add(button);
}
public void UselessFunction() {
button.Visible=false;
button.Visible=true;
}
}