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;
}
} 2 1575
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******** ******@TK2MSFTN GP10.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.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; } }
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_P roc() {
UserControl1 uc=new UserControl1();
uc.CreateContro l();
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.mach in AT dot.state.fl.us > a
écrit dans le message de news:%2******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP10.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.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; } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 know what multi-page tiff controls were used to create the
Microsoft Office Document Imaging app or where I can buy similar .Net
controls that will allow me to view a multi-page TIFF (zoom in/out, display
thumbnails, rotate current frame left/right...
|
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 web
page dynamically. To do this, First I including a web page
(MainPage.aspx) and I made form tag to Runat=Server. I included one
table tag and also made this table Runat=Server side. Second I created
three Web User Controls e.g....
|
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
page dynamically. To do this, First I including a web page
(MainPage.aspx) and I made form tag to Runat=Server. I included one
table tag and also made this table Runat=Server side. Second I created
three Web User Controls e.g. wucCustomerInfo.ascx,
|
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" Width="100"></asp:TextBox>
<asp:DropDownList id="ddlWidthIssue" runat="server"
Width="100"></asp:DropDownList>
Browser like Firefox and I'll imagine many others are not sizing correctly
the controls (.net is removing the with property during the rendering). I
tried...
|
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.
Each question is displayed and answered, then result written to a SQL table.
Then the next question is read from a table and displayed using the
load_page event again.
The questions display and function perfectly. The user anwers the question...
| |
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 GridView that needs to be dynamically designed, depending on what
collection of fields our uses want to edit for their product data. We have
400+ fields of information per product so they're selecting a subset of those
fields to edit.
|
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 application.
What should happen, is that the main MDI form should close, taking the child
forms with it. There is code to loop through the child forms, remove the
controls on each of them, and then close the form, but this code should
execute only...
|
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 expand as it reads in records, so I will need to preserve
the current contents.
For example, I currently do this in Visual Foxpro.
Public laList(1,5)
|
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 getting the radiobuttons
out of the group box.
/// area where radio buttons added to groupbox
/// this code is wrapped in another loop creating group boxes
|
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 I'll make the inactive forms
invisible but this is creating a memory corruption problem when user close
the form2 or form3 and not the formMain.
My main form has a Next button which makes the main form invisible and
starts a new form which I'll...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |