473,396 Members | 1,756 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,396 software developers and data experts.

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

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

--
"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
Aug 23 '06 #1
4 3813
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
}

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Aug 23 '06 #2
"Redgum" <Re****@discussions.microsoft.comwrote in message
news:13**********************************@microsof t.com...
>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/lib...undworker.aspx

-- Alan
Aug 23 '06 #3
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" wrote:
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
}

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Aug 26 '06 #4
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" wrote:
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" wrote:
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
}

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

Aug 26 '06 #5

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

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
2
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
2
by: Mirco Wahab | last post by:
After reading through some (open) Intel (CPU detection) C++ source (www.intel.com/cd/ids/developer/asmo-na/eng/276611.htm) I stumbled upon a sketchy use of multibyte characters - - - - - - - - -...
1
by: ITHELP85 | last post by:
Hello I am trying to get a table attribute to calculate the result of 2 attribute in another table. I am using SQL Server 2005. I got another table to do this and it works fine, but the only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.