Boilerplate example for updating GUI inside a C# Thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.Invoke(new UpdateTextCallback(this.UpdateText),
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?
 
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?
 
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 said:
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
 
I put it in a Try/Catch and get the following:

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

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?
 
Okay thanks for the tip! I put a while loop that that was conditional on
TextBox.IsHandleCreated being FALSE right before my Invoke call and
that did the trick!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top