How do I code a multi-threaded status update text box?

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

Guest

I have a windows forms project that uses multiple tabs. On the first tab I
have a rich text box that I'd like to be able to post text results from
various operational threads - such as "Starting task such-n-such..." and
'completed task." or Debugging info from other threads which may time-out
connecting to a database etc.

Most of the example code I've found only uses console.writeline() -- and for
some reason, the post-back delegate approach makes the strings print twice,
and it still causes a cross-threading warning in the IDE.

So, for those of you who have probably done this a dozen times - can you
help me please? I'd like to be able to use ThreadPool and dedicated threads.

Environment: Visual Studio 2005 Enterprise Architect 2005
 
I may have missed the trick here. Isn't it a case of using in the form
a method with this kind of code?

if (InvokeRequired)
BeginInvoke...
else
{
update text here
}
 
Redgum said:
I have a windows forms project that uses multiple tabs. On the first tab I
have a rich text box that I'd like to be able to post text results from
various operational threads - such as "Starting task such-n-such..." and
'completed task." or Debugging info from other threads which may time-out
connecting to a database etc.

The BackgroundWorker class makes this kind of thing very simple. It does
all the marshalling to the UI thread for you.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

-- Alan
 
Mike M:

That was the method I refered to in my post - that prints the text twice and
does not avoid the cross-thread problem.

Here's a snippet from the Print() method in the main form:

public void Print(string text)
{
if (txtStatus.InvokeRequired)
{
SetTextCallback prt_cb = new SetTextCallback(Print);
this.BeginInvoke(prt_cb, new object[] { text });
Thread.Sleep(4000); // inserted to test dupe occurence (yep)

}

txtStatus += text;
...
 
Oh geeze.

I see the problem...

It needs a "return" in that postback trap.... DOH!

*slaps forehead*

Just tested it with a return after the postback - it seems to work. I'm
using the backgroundworker thread as well. Works so far with both types -
ThreadPool & Background.

*slaps forehead again*
--
"The Madman is not the man who has lost his reason, the Madman is the man
who has lost everything except his reason."

-- G.K. Chesterton


Redgum said:
Mike M:

That was the method I refered to in my post - that prints the text twice and
does not avoid the cross-thread problem.

Here's a snippet from the Print() method in the main form:

public void Print(string text)
{
if (txtStatus.InvokeRequired)
{
SetTextCallback prt_cb = new SetTextCallback(Print);
this.BeginInvoke(prt_cb, new object[] { text });
Thread.Sleep(4000); // inserted to test dupe occurence (yep)

}

txtStatus += text;
...

--
"The Madman is not the man who has lost his reason, the Madman is the man
who has lost everything except his reason."

-- G.K. Chesterton


Michael Moreno said:
I may have missed the trick here. Isn't it a case of using in the form
a method with this kind of code?

if (InvokeRequired)
BeginInvoke...
else
{
update text here
}
 
Back
Top