473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem creating controls with multi-threading

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;
}
}
Nov 16 '05 #1
2 1568
Hi,

You should handle all the UI related code in the main thread, if you want to
create a control do so in the main thread, you can assure that a method runs
on it using Control.Invoke ( it will run the method in the thread that
created the control originally, as you only created in the main thread that
does the trick ).

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ludovic SOEUR" <Lu***********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP10.phx.gbl...
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;
}
}

Nov 16 '05 #2
Thanks for your answer.
You were right, the control must be created in the main thread using
Control.Invoke.
But, after a lot of work, I found there was something more tricky : before
adding the control, it MUST be created with function CreateControl in the
main thread to ensure that all window handles are owned by the main thread.

Here is the source code of the function called by Invoke in order to create
and attach properly the control :

private UserControl1 CreateControl_Proc() {
UserControl1 uc=new UserControl1();
uc.CreateControl();
Controls.Add(uc);
return uc;
}

Thanks Ignacio for your answer that helped me to find a solution.
Sincerely,

Ludovic Soeur.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> a
écrit dans le message de news:%2****************@tk2msftngp13.phx.gbl...
Hi,

You should handle all the UI related code in the main thread, if you want to create a control do so in the main thread, you can assure that a method runs on it using Control.Invoke ( it will run the method in the thread that
created the control originally, as you only created in the main thread that does the trick ).

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ludovic SOEUR" <Lu***********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP10.phx.gbl...
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;
}
}


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
I need to duplicate some of the multi-page tiff and thumbnail viewing functionality found in the Microsoft Office Document Imaging application for an in-house company application. Does anyone...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
5
by: Luis Fajardo | last post by:
I'm expiriencing a basic HTML appearance problem with ASP.NET. Following a couple of sample tags to ilustrate my problem: <asp:TextBox id="txtWidthIssue" runat="server"...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
by: Dino | last post by:
I need to create a public multi-dim array that can hold a mixture of string and int values. I will need to search this array for a value and return a value from a subscript. This array will...
8
by: johnmmcparland | last post by:
Hi all, my program is trying to add group boxes with radio buttons at run time. While at one point it was able to draw the group boxes without the radio buttons, now it encounters problems just...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.