473,398 Members | 2,335 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,398 software developers and data experts.

Initiating GUI control creation from a non-GUI thread

Hi,

I have a GUI application that responds to events coming from an externaql
application. In the implementing code for the event I need to create a GUI
control and "parent" the control into the GUI. Doing this directly fails, it
is not allowed to parent a control that was not created on the main GUI
thread into the main GUI through another thread (an exception is thrown on
"control.Parent = container;").

So I have to create the control on the main thread but I don't want to
create a pool of controls upfront because there is no telling how many I
will need (it depends on the type of events that may be recieved).

So, the control needs to be created on the main thread yet this action has
to be initiated just in time from the external thread. In any Win32
application I would implement the control creation code in a message handler
of the main form, invoke it using SendMessage with WM_COMMAND and some
custom message from the other thread and retrieve the control pointer
through a private variable in the main form.

Now I could import SendMessage and hack around with some handles but I would
rather do it the .NET way.

What is the .NET way?

Regards,

Martin.
Nov 15 '05 #1
5 2092
TB

----- Martin Maat [EBL] wrote: ----

Hi

I have a GUI application that responds to events coming from an externaq
application. In the implementing code for the event I need to create a GU
control and "parent" the control into the GUI. Doing this directly fails, i
is not allowed to parent a control that was not created on the main GU
thread into the main GUI through another thread (an exception is thrown o
"control.Parent = container;")

So I have to create the control on the main thread but I don't want t
create a pool of controls upfront because there is no telling how many
will need (it depends on the type of events that may be recieved)

So, the control needs to be created on the main thread yet this action ha
to be initiated just in time from the external thread. In any Win3
application I would implement the control creation code in a message handle
of the main form, invoke it using SendMessage with WM_COMMAND and som
custom message from the other thread and retrieve the control pointe
through a private variable in the main form

Now I could import SendMessage and hack around with some handles but I woul
rather do it the .NET way

What is the .NET way

Your main form should declare one or more delegates that will take the responsibility for creating the controls. Your worker thread should then use Form.Invoke to call those delegates so that the delegates run in the owning thread of the form

Look into Invoke( ), BeginInvoke( )/EndInvoke( ), and the C# delegate mechanism for more information.

-- T
Nov 15 '05 #2
"TB" <tb*********@kaxy.NOSPAM.com> wrote in message
news:DE**********************************@microsof t.com...
Your main form should declare one or more delegates that will take
the responsibility for creating the controls. Your worker thread should
then use Form.Invoke to call those delegates so that the delegates run
in the owning thread of the form.
Look into Invoke( ), BeginInvoke( )/EndInvoke( ), and the C# delegate

mechanism for more information.

Thank you very much for the quick response, this is indeed what I needed and
it works now. Implementing it raised one other question though. This is my
code now:
// delegate declaration in main form
private delegate RichTextBox RtbCreationDelegate();

// part run by external thread
RtbCreationDelegate del = new RtbCreationDelegate(CreateRtb);
RichTextBox rtb = (RichTextBox)Invoke(del);

// control creating method in main form
private RichTextBox CreateRtb()
{
RichTextBox rtb = new RichTextBox();
rtb.Parent = pnlMiddle;
rtb.Dock = DockStyle.Fill;
rtb.ReadOnly = true;
return rtb;
}
and like I said it works nice. I had to make the delegate return the
RichTextBox as a return value though. I initially had the delegate declared
like this:

private delegate void RtbCreationDelegate(ref RichTextBox rtb);

and then I had to pass the uninitialized rtb by ref but I could not do it. I
tried
RichTextBox rtb = null;
RtbCreationDelegate del = new RtbCreationDelegate(CreateRtb);
object[] args = new object[1];
args[0] = rtb;
Invoke(del, args);
which doesn't work, I am not passing rtb by ref so it doesn't come back. And
the address operator is not allowed on rtb (RichTextBox* cannot be
implicitely converted to object). How can I pass rtb by ref through the args
array element?

This is probably really simple and straightforward, but my curiosity
outweighs my sense of shame.

Martin.
Nov 15 '05 #3
Martin,
what does EBL in your signature mean? Is this some form of certification
that I must pursue...

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Martin Maat [EBL]" <du***@somewhere.nl> wrote in message
news:10*************@corp.supernews.com...
"TB" <tb*********@kaxy.NOSPAM.com> wrote in message
news:DE**********************************@microsof t.com...
Your main form should declare one or more delegates that will take
the responsibility for creating the controls. Your worker thread should
then use Form.Invoke to call those delegates so that the delegates run
in the owning thread of the form.
Look into Invoke( ), BeginInvoke( )/EndInvoke( ), and the C# delegate mechanism for more information.

Thank you very much for the quick response, this is indeed what I needed

and it works now. Implementing it raised one other question though. This is my
code now:
// delegate declaration in main form
private delegate RichTextBox RtbCreationDelegate();

// part run by external thread
RtbCreationDelegate del = new RtbCreationDelegate(CreateRtb);
RichTextBox rtb = (RichTextBox)Invoke(del);

// control creating method in main form
private RichTextBox CreateRtb()
{
RichTextBox rtb = new RichTextBox();
rtb.Parent = pnlMiddle;
rtb.Dock = DockStyle.Fill;
rtb.ReadOnly = true;
return rtb;
}
and like I said it works nice. I had to make the delegate return the
RichTextBox as a return value though. I initially had the delegate declared like this:

private delegate void RtbCreationDelegate(ref RichTextBox rtb);

and then I had to pass the uninitialized rtb by ref but I could not do it. I tried
RichTextBox rtb = null;
RtbCreationDelegate del = new RtbCreationDelegate(CreateRtb);
object[] args = new object[1];
args[0] = rtb;
Invoke(del, args);
which doesn't work, I am not passing rtb by ref so it doesn't come back. And the address operator is not allowed on rtb (RichTextBox* cannot be
implicitely converted to object). How can I pass rtb by ref through the args array element?

This is probably really simple and straightforward, but my curiosity
outweighs my sense of shame.

Martin.

Nov 15 '05 #4
"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
what does EBL in your signature mean? Is this some form of certification
that I must pursue...


Some weeks ago there was a thread on MVP and what it meant. It is typically
American to use titles and hang signs of competence over your work spot in
the office. Most Europeans would frown on the habit, it's a culture thing.
We would acknowledge that it will be practicle and appropriate at times to
do so but at the same time we feel uncomfortable doing it ourselves. It
feels embarrassing, in Europe people would typically be ridiculed for
overdisplaying their education. Like "one doesn't display wealth", one
should not display education too much. It probably goes back to Calvinism.
Pride is not a virtue, "one should be modest and grateful".

While reading that thread on MVP I got the idea to join in and put a title
behind my name too. It is a joke, EBL stands for "Eenvoudige Boeren Lul"
which is Dutch. The translation would be "simple dickhead from the
countryside", it is a qualification one typically assigns oneself when one
is expressing one doesn't know. "Hey, what do I know? I'm just one of those
EBL's like the most of us".

The abbreviation is not widely recognized but most Dutch people would be
able to figure it out and appreciate the joke. I could have used LDB too
which would be more easily recognized and has similar meaning though would
be harder to translate.

I will take it out now, once everyone knows it's not half that funny
anymore.

Martin.
Nov 15 '05 #5
TB

----- Martin Maat [EBL] wrote: ----
"TB" <tb*********@kaxy.NOSPAM.com> wrote in messag
news:DE**********************************@microsof t.com..
Your main form should declare one or more delegates that will tak
the responsibility for creating the controls. Your worker thread shoul
then use Form.Invoke to call those delegates so that the delegates ru
in the owning thread of the form
Look into Invoke( ), BeginInvoke( )/EndInvoke( ), and the C# delegat

mechanism for more information

Thank you very much for the quick response, this is indeed what I needed an
it works now. Implementing it raised one other question though. This is m
code now

[snip]

How can I pass rtb by ref through the arg
array element

This is probably really simple and straightforward, but my curiosit
outweighs my sense of shame

Hmmm... good question! I haven't actually tried this to see if it works, but it should..
Declare a new class that just encapsulates a RichTextBox..

class RTBWrappe

public RichTextBox rtb

RTBWrapper( ) { rtb = null;
Now, setup your delegate to take an RTBWrapper instead of a RichTextBox. The delegate can access the internal RichTextBox and assign it an actual control and when you get control back in your worker thread you should be able to access the now non-null value. Something like this..

RTBWrapper wrapper = new RTBWrapper()
Invoke(new RtbCreationDelegate(CreateRtb), new object[] {wrapper})
/
// Now you can reference wrapper.rtb for the actual contro

This does seem a little kludgey though... This is basically "boxing" the RichTextBox explicitly in this auxiliary class. Perhaps one can box a reference type in a generic Object in the same way one boxes a value type, but I'm not sure how that would work

-- TB
Nov 15 '05 #6

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

Similar topics

0
by: Richard L Rosenheim | last post by:
I'm looking to display a page and then initiate the downloading of a file. Basically trying to display a page that says the download will begin shortly, and if it doesn't, click here. Like what...
2
by: charliewest | last post by:
I need to create textboxes in real-time, the actual number of which is determine by a result from a database query. I have been able to create the controls, and then add them to the ASPX page....
5
by: Jay | last post by:
I have a situation where the user clicks on a button in a DataGrid to launch a popup window via javascript. In the popup window the user does some things that result in changes to the underlying...
3
by: Calvin Lai | last post by:
Hi all, I have a problem in creating web user control *programatically*. Problem as follows: 1. WebUserControl1 contains a server side table named Table1 2. WebForm1 contain a server panel...
3
by: Bernie Yaeger | last post by:
I'm trying to create a custom control that has 2 listviews, in view smallicon. I'm using these with code that makes them drag/drop controls, so what I am after is a control that does drag/drop...
0
by: Paul | last post by:
Hi! I have been wondering about a design issue for some time now and hope someone can help sort this one out for me. Say you have to create some webcontrols dynamically. Each controls creation...
4
by: Charles Zhang | last post by:
I created a custom server control whose rendering logic relies on session variables and cookies. It works just fine at run time. The problem is at the design time, because session variables and...
11
by: Nick Gilbert | last post by:
Hi, How can I create a custom control which will wrap its content in a header and footer? eg: Is it possible to create a .NET user control which can surround other controls? eg:...
0
by: AmyHanson | last post by:
I am new to mobile development and am having some trouble sources for the tasks I need to perform. I have been looking around and I can find plenty of information on copying the file initiating on...
1
by: brixdotnet | last post by:
Hi there! I made own user control for displaying and editing content. In Page_Load of this control is checked if user is authorized for editing if so, button for editing is shown, otherwise...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
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...
0
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...
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.