473,806 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boilerplate example for updating GUI inside a C# Thread

Hello,

99% of the time I can find information by reading newsgroup posts or reading
books but I am having a hard time putting together information on how to
update
a control inside a C# thread. I am running .NET 2.0 and apparently alot of
newsgroup posts center around pre-V2.0.

Basically I have a thread that I start and inside the thread I want to
update a TextBox that is on my GUI. If someone can supply a tried and true
clearcut example it would be greatly appreciated!

-Peter
May 24 '06 #1
8 3076
Nothing about this has changed since .NET 1.1 to my knowledge.

I google'd "C# update gui thread" and first result was:
http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx

--
Adam Clauss

"Peter S." <Pe****@discuss ions.microsoft. com> wrote in message
news:6A******** *************** ***********@mic rosoft.com...
Hello,

99% of the time I can find information by reading newsgroup posts or
reading
books but I am having a hard time putting together information on how to
update
a control inside a C# thread. I am running .NET 2.0 and apparently alot of
newsgroup posts center around pre-V2.0.

Basically I have a thread that I start and inside the thread I want to
update a TextBox that is on my GUI. If someone can supply a tried and true
clearcut example it would be greatly appreciated!

-Peter

May 24 '06 #2
I actually found this and thought this was one of the best however Visual
Studio doesn't seem to like the portion of the answer that provides the
following line:
m_TextBox.Inv oke(new UpdateTextCallb ack(this.Update Text),
new object[]{”Text generated on non-UI thread.”});

The compiler doesn't like "this" to be used. If I remove that I get an error
about not liking "UpdateText " as a valid object. Should this work in
the callback function of the thread?
"Adam Clauss" wrote:
Nothing about this has changed since .NET 1.1 to my knowledge.

I google'd "C# update gui thread" and first result was:
http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx

--
Adam Clauss

"Peter S." <Pe****@discuss ions.microsoft. com> wrote in message
news:6A******** *************** ***********@mic rosoft.com...
Hello,

99% of the time I can find information by reading newsgroup posts or
reading
books but I am having a hard time putting together information on how to
update
a control inside a C# thread. I am running .NET 2.0 and apparently alot of
newsgroup posts center around pre-V2.0.

Basically I have a thread that I start and inside the thread I want to
update a TextBox that is on my GUI. If someone can supply a tried and true
clearcut example it would be greatly appreciated!

-Peter


May 24 '06 #3
Okay I think I figured it out. I stuck with this example and spent some
time on it an realized my thread routine was static. Once I removed that
configuration I was able to reference the delegate function. I must have
seen an example that made the thread routine static and assumed that
was the norm. Sound okay?

"Adam Clauss" wrote:
Nothing about this has changed since .NET 1.1 to my knowledge.

I google'd "C# update gui thread" and first result was:
http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx

--
Adam Clauss

"Peter S." <Pe****@discuss ions.microsoft. com> wrote in message
news:6A******** *************** ***********@mic rosoft.com...
Hello,

99% of the time I can find information by reading newsgroup posts or
reading
books but I am having a hard time putting together information on how to
update
a control inside a C# thread. I am running .NET 2.0 and apparently alot of
newsgroup posts center around pre-V2.0.

Basically I have a thread that I start and inside the thread I want to
update a TextBox that is on my GUI. If someone can supply a tried and true
clearcut example it would be greatly appreciated!

-Peter


May 24 '06 #4
One more problem. This example now works flawlessly. The only
problem is that the executable crashes when I run it in Visual *WITHOUT*
debugging turned on. If I select "Start with debugging" all is fine!?!?!?!
Am I still on the right track with this solution??????? ??

"Adam Clauss" wrote:
Nothing about this has changed since .NET 1.1 to my knowledge.

I google'd "C# update gui thread" and first result was:
http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx

--
Adam Clauss

"Peter S." <Pe****@discuss ions.microsoft. com> wrote in message
news:6A******** *************** ***********@mic rosoft.com...
Hello,

99% of the time I can find information by reading newsgroup posts or
reading
books but I am having a hard time putting together information on how to
update
a control inside a C# thread. I am running .NET 2.0 and apparently alot of
newsgroup posts center around pre-V2.0.

Basically I have a thread that I start and inside the thread I want to
update a TextBox that is on my GUI. If someone can supply a tried and true
clearcut example it would be greatly appreciated!

-Peter


May 24 '06 #5
Peter S. wrote:
One more problem. This example now works flawlessly. The only
problem is that the executable crashes when I run it in Visual *WITHOUT*
debugging turned on. If I select "Start with debugging" all is fine!?!?!?!
Am I still on the right track with this solution??????? ??


Peter,

Is there any chance the code is placed somewhere that it tries to invoke
on the control before it is displayed (such as in the constructor) ?

Just a thought...

Dan
May 24 '06 #6
I put it in a Try/Catch and get the following:

System.InvalidO perationExcepti on: Invoke or BeginInvoke cannot be called on
a control until the window handle has been created. at
System.Windows. Forms.Control.M arshalledInvoke (....

So that seems to be it, the thread runs at a point where the control isn't
full created. How can I be sure that the window handle has been created?

"Dan Manges" wrote:
Peter S. wrote:
One more problem. This example now works flawlessly. The only
problem is that the executable crashes when I run it in Visual *WITHOUT*
debugging turned on. If I select "Start with debugging" all is fine!?!?!?!
Am I still on the right track with this solution??????? ??


Peter,

Is there any chance the code is placed somewhere that it tries to invoke
on the control before it is displayed (such as in the constructor) ?

Just a thought...

Dan

May 24 '06 #7
Peter S. wrote:
How can I be sure that the window handle has been created?


Use the load event for the form.
May 24 '06 #8
Okay thanks for the tip! I put a while loop that that was conditional on
TextBox.IsHandl eCreated being FALSE right before my Invoke call and
that did the trick!

"Dan Manges" wrote:
Peter S. wrote:
One more problem. This example now works flawlessly. The only
problem is that the executable crashes when I run it in Visual *WITHOUT*
debugging turned on. If I select "Start with debugging" all is fine!?!?!?!
Am I still on the right track with this solution??????? ??


Peter,

Is there any chance the code is placed somewhere that it tries to invoke
on the control before it is displayed (such as in the constructor) ?

Just a thought...

Dan

May 24 '06 #9

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

Similar topics

2
4921
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.Thesearepartoftheproblem. I want to have something running in the background, while the GUI is updating.I'vedonethatbeforewithoutaproblem,however,now,Ineedto pass variables to the separate Thread or Runnable that I'm using.I'm using something like this: //Other code setting things up and updating GUI //Variables...
0
1303
by: Eric Williams | last post by:
Hello all, I'm working on a little application using PyQt. The main window is just a dialog with 2 progress bars and a few labels. In its original form, the dialog started, then ran a function which downloaded a directory full of files from an ftp server, using an ftplib.FTP object's retrbinary function. http://xoffender.de/Downloads/screenie4.png Now, inside the callback function of the retrbinary call, I was calling the
15
2601
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html ****************************************************************************** Hi fellow Python coders, I often find myself writing:: class grouping:
0
2639
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
18
2291
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object): def __init__(self, *args, **keyword_args): self.__dict__.update(
1
1408
by: Joshua Russell | last post by:
Firstly my main method is like this: static void Main(string args) { // New Form Thread FormHandler myFormHandler = new FormHandler(); ThreadStart myThreadStart = new ThreadStart(myFormHandler.ShowForm); Thread formWorkerThread = new Thread(myThreadStart); formWorkerThread.Start();
10
8447
by: Curious | last post by:
Hi, I have a thread running, and at a point in this thread I am calling a method to update GUI controls. From the net I have read that updating GUI controls from the worker thread is bad practice. Thread myThread = new Thread(new ThreadStart(Worker));... myThread.Start();
4
7618
by: directory | last post by:
hey guys, I've got a weird one for ya....i have a form which takes user input in the form of textbox's etc. It then grabs some details from a file and updates some of the labels with some info like numbers etc. Anyway, i'm just updating the field properties like lblnumber.Text = variable1; Now the weird thing is that...when i run the app and it cycles through
6
3649
Xx r3negade
by: Xx r3negade | last post by:
Consider this: I have the class foo, with the public method thread(), and the private method bar() In the thread() method, I would like to create a new thread, using pthread_create(), that points to bar(). Could someone tell me how to do this? I have the following code: #include <iostream>
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10620
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, 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...
0
10369
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 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...
0
10110
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 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...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
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...

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.