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

Multithread writes to a Rich Text Box

Hello,

Sorry for the long-winded dissertation - but, I have an application where I
need to write text to a rich text box using a common method, callable from
anywhere in my application. The color of the text will depend upon the first
argument and the text will be contained in the second argumet. As far as I
know, my application is not multithreaded - at least I don't knowingly start
any other threads. Since I don't know what the heck I'm doing, I found an
application that did this type of thing and merely "borrowed" the code to do
it. The code is shown below, with a couple of changes, which I will describe.

My first question involves why the code is written using Invoke rather than
just a simple method call? I do know that Invoke should (must?) be used when
doing multithreading, but I wasn't aware I was doing any multithreading.
However, if I remove the Invoke "wrapper" from around the code it won't even
compile - so I left the "wrapper" in place!

Second, notice that I have a test for the msg parameter being null. This
should never happen but it sometimes does. I've single stepped through it
and sometimes, although msg is not null just prior to entering the Invoke
block, it mysteriously becomes null when that block is entered.

The next issue is the color. Note that I am setting the color with
parameter "msgtype". Most of the time this also works fine. However, once
in a while the parameter value and the font's color property are okay right
up to the rtfTerminal.AppendText statement. But as that statement gets
executed both the parameter and the font property change and the text gets
printed in the wrong color. Sometimes this change occurs in the middle of
the string that's being printed.

The final issue is the jitter that accompanies the ScrollToCaret() method if
the entire message is written as one string followed by a single call to
ScrollToCaret(). It makes no sense to me why doing it the way I have done -
character at a time - should work differently, but it does. I think this is
probably a separate issue from the previous two.

So, my final suspicion on this is that the code I "borrowed" probably has
some inherent philosophical problem that is currently beyond my knowledge.
Is there a more appropriate way to implement a common method to write to a
rich text box?

Thanks,
Ray

public void Log(LogMsgType msgtype, string msg)
{
rtfTerminal.Invoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;
rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont,
FontStyle.Bold);
rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
rtfTerminal.Font = new Font(rtfTerminal.SelectionFont,
FontStyle.Bold);
if (msg != null)
{
// This loop prevents scrolling on every character, which causes
jitter.
foreach (char ch in msg)
{
if (ch != '\r')
{
rtfTerminal.AppendText(ch.ToString());
if (ch == '\n')
rtfTerminal.ScrollToCaret();
}
}
Application.DoEvents();
}
else
rtfTerminal.AppendText("\n\n**** ERROR: NULL MESSAGE ****\n\n");
}
));
}

May 10 '07 #1
2 3067
Ray Mitchell <Ra*****************@MeanOldTeacher.comwrote:
Sorry for the long-winded dissertation - but, I have an application where I
need to write text to a rich text box using a common method, callable from
anywhere in my application. The color of the text will depend upon the first
argument and the text will be contained in the second argumet. As far as I
know, my application is not multithreaded - at least I don't knowingly start
any other threads. Since I don't know what the heck I'm doing, I found an
application that did this type of thing and merely "borrowed" the code to do
it. The code is shown below, with a couple of changes, which I will describe.

My first question involves why the code is written using Invoke rather than
just a simple method call? I do know that Invoke should (must?) be used when
doing multithreading, but I wasn't aware I was doing any multithreading.
However, if I remove the Invoke "wrapper" from around the code it won't even
compile - so I left the "wrapper" in place!
Unfortunately you haven't shown any of the rest of your code, so we've
no idea why you might be using multiple threads. If you dump the stack
trace out somewhere (eg the rich text box!) having captured it *before*
the call to Invoke (i.e. not within the anonymous method) then you may
well see a bit more of what's going on.

Using Invoke is correct here. The call to Application.DoEvents() is
unnecessary, however, and I'm not at all sure about appending a single
character at a time.
Second, notice that I have a test for the msg parameter being null. This
should never happen but it sometimes does. I've single stepped through it
and sometimes, although msg is not null just prior to entering the Invoke
block, it mysteriously becomes null when that block is entered.
Hmm. That just shouldn't happen - I can't think of any reason off-hand
why it would. Again, we'd need more information.
The next issue is the color. Note that I am setting the color with
parameter "msgtype". Most of the time this also works fine. However, once
in a while the parameter value and the font's color property are okay right
up to the rtfTerminal.AppendText statement. But as that statement gets
executed both the parameter and the font property change and the text gets
printed in the wrong color. Sometimes this change occurs in the middle of
the string that's being printed.
Again, that sounds very odd.
The final issue is the jitter that accompanies the ScrollToCaret() method if
the entire message is written as one string followed by a single call to
ScrollToCaret(). It makes no sense to me why doing it the way I have done -
character at a time - should work differently, but it does. I think this is
probably a separate issue from the previous two.
Yes, I suspect so.

<snip>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #2
Ray,

I would address the issue of multithreading first. Since you are not
making this call on another thread, I would say to take out the call to
Invoke. In regards to the part that has:

delegate
{
// Code here.
}

You can take the delegate and the brackets away, and just call the code
directly, and it should work.

You should also probably take out the call to DoEvents, as it's just
pointless in this case.

As for the msgtype parameter changing (the colors and whatnot) if you
don't have a multithreaded program, then nothing should be changing this
once you get into your method.

Also, if msg is null, or blank, then you should look at the call stack
when this method is called to try and determine the path the code took to
make msg null. Parameters don't just disappear.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:DC**********************************@microsof t.com...
Hello,

Sorry for the long-winded dissertation - but, I have an application where
I
need to write text to a rich text box using a common method, callable from
anywhere in my application. The color of the text will depend upon the
first
argument and the text will be contained in the second argumet. As far as
I
know, my application is not multithreaded - at least I don't knowingly
start
any other threads. Since I don't know what the heck I'm doing, I found an
application that did this type of thing and merely "borrowed" the code to
do
it. The code is shown below, with a couple of changes, which I will
describe.

My first question involves why the code is written using Invoke rather
than
just a simple method call? I do know that Invoke should (must?) be used
when
doing multithreading, but I wasn't aware I was doing any multithreading.
However, if I remove the Invoke "wrapper" from around the code it won't
even
compile - so I left the "wrapper" in place!

Second, notice that I have a test for the msg parameter being null. This
should never happen but it sometimes does. I've single stepped through it
and sometimes, although msg is not null just prior to entering the Invoke
block, it mysteriously becomes null when that block is entered.

The next issue is the color. Note that I am setting the color with
parameter "msgtype". Most of the time this also works fine. However,
once
in a while the parameter value and the font's color property are okay
right
up to the rtfTerminal.AppendText statement. But as that statement gets
executed both the parameter and the font property change and the text gets
printed in the wrong color. Sometimes this change occurs in the middle of
the string that's being printed.

The final issue is the jitter that accompanies the ScrollToCaret() method
if
the entire message is written as one string followed by a single call to
ScrollToCaret(). It makes no sense to me why doing it the way I have
done -
character at a time - should work differently, but it does. I think this
is
probably a separate issue from the previous two.

So, my final suspicion on this is that the code I "borrowed" probably has
some inherent philosophical problem that is currently beyond my knowledge.
Is there a more appropriate way to implement a common method to write to a
rich text box?

Thanks,
Ray

public void Log(LogMsgType msgtype, string msg)
{
rtfTerminal.Invoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;
rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont,
FontStyle.Bold);
rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
rtfTerminal.Font = new Font(rtfTerminal.SelectionFont,
FontStyle.Bold);
if (msg != null)
{
// This loop prevents scrolling on every character, which
causes
jitter.
foreach (char ch in msg)
{
if (ch != '\r')
{
rtfTerminal.AppendText(ch.ToString());
if (ch == '\n')
rtfTerminal.ScrollToCaret();
}
}
Application.DoEvents();
}
else
rtfTerminal.AppendText("\n\n**** ERROR: NULL MESSAGE
****\n\n");
}
));
}

May 10 '07 #3

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

Similar topics

3
by: Alfredo Agosti | last post by:
Hi folks, I have an Access 2000 db with a memo field. Into the memo field I put text with bold attributes, URL etc etc What I need to to is converting the rich text contained into the memo...
0
by: Alice | last post by:
Hello I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
1
by: PC User | last post by:
I found this Rich Text Editor and I've been trying to recreate it in my own application. I've had trouble with the COMCTL.ImageListCtrl and the COMCTL.Toolbar to recreate the toolbar. And I've...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
0
by: ray well | last post by:
hi, my app has two parallel rich text boxes containing the same content in 2 different languages. the lines parallel each other, line #3 in english contains the same content as line #3 in...
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.