dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] Richtextbox RTF - I learned something

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Resolved [RESOLVED] Richtextbox RTF - I learned something

    Working on an application that reads RTF. In the past when working with Richtextboxes we have found that applications are faster using a RTB that mirrors a RTB on the UI. The mirror RTB is created and disposed on a thread.

    What learned is that if I do this

    bkgRTB.Rtf = rtbOnTheUI.Rtf

    where:
    • bkgRTB.Rtf - mirror RTB
    • rtbOnTheUI.Rtf - RTB on the UI


    The background RTB thinks / is on the UI. It is manifested by CrossThread errors.

    The fix was to load the RTF into a string and then load each of the RTB's .Rtf property from that.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,752

    Re: Richtextbox RTF - I learned something

    This isn't really anything specific to RichTextBox's. If you create two controls on different threads then any code that access a property of both controls must be executed on a thread that does not the handle of at least one of the controls, so a cross-thread exception is inevitable. It doesn't matter whether it is the Rtf properties of RichTextBoxes, the Text properties of TextBoxes, the Value properties of DateTimePickers, the Value properties of NumericUpDowns or something else. The very same rule applies here as with any other control in any other multi-threaded application: when you access a member of a control, you must do it on the thread on which the control's handle was created. If you have two RichTextBoxes that were created on different threads then any of their properties - not just Rtf - must be access only on their respective creating threads.

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Richtextbox RTF - I learned something

    Not sure I understand that. I'll test the text property tomorrow. If I wasn't clear, the background RTB was created on the background thread. When I assigned the RTF property from the UI RTB I did so from the UI. That worked. It was when I accessed other properties of the background RTB that errors occurred.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Richtextbox RTF - I learned something

    Quote Originally Posted by jmcilhinney View Post
    This isn't really anything specific to RichTextBox's. If you create two controls on different threads then any code that access a property of both controls must be executed on a thread that does not the handle of at least one of the controls, so a cross-thread exception is inevitable. It doesn't matter whether it is the Rtf properties of RichTextBoxes, the Text properties of TextBoxes, the Value properties of DateTimePickers, the Value properties of NumericUpDowns or something else. The very same rule applies here as with any other control in any other multi-threaded application: when you access a member of a control, you must do it on the thread on which the control's handle was created. If you have two RichTextBoxes that were created on different threads then any of their properties - not just Rtf - must be access only on their respective creating threads.
    In the following rtb is a richtextbox created on a background thread, and rtbIN is part of the UI. When I assign the .Rtf property I get errors later in the code when I access rtb properties, e.g. Dim lns() As String = rtb.Lines . When I assigned the .Text property the code runs fine. Seems odd to me.

    Code:
                     'in the background thread
                      Dim rtb As New RichTextBox
    
                      Me.Invoke(Sub()
                          'rtb.Rtf = rtbIN.Rtf 'causes Cross-thread operation not valid later
                          rtb.Text = rtbIN.Text 'This works
                      End Sub)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,342

    Re: Richtextbox RTF - I learned something

    I would assume that
    rtb.Rtf = rtbIn.Rtf
    isn't making a copy of the text, it is copy of a reference, so both rtbs are referencing the same Gui object.
    Later when you try to use that reference from the other thread, you get the error because you're trying to reference an object created on the Gui thread.

    The .Text might work, because .Text is a String, and a String is treated as a Value type, not a reference type. So, both rtb's may be referencing the same string, but because it is treated as a value type, and is immutable, it won't cause a object Cross-thread operation error.

    In either case, since it seems like you intend to make a copy of the data, rather than a reference to the data, you shouldn't be doing an assignment like you are. I don't know if the .Rtf has a clone method to create a copy or not. Should look it up, but not curious enough.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,410

    Re: Richtextbox RTF - I learned something

    Saying Rtf might not be a string is like saying JSON might not be a string... It is... You really should have looked it up. 15 seconds would have saved you some grief.


    It does seem odd though. The only thing I can think of is that there's something going on with the internals of the RTB in its rendering. With .Text, it just simply displays the text that's passed to it. With the .Rtf property though, it has to parse it and render it with its formatting. So clearly there's something else going on. But what it is, I couldn't say.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Richtextbox RTF - I learned something

    Quote Originally Posted by passel View Post
    I would assume that
    rtb.Rtf = rtbIn.Rtf
    isn't making a copy of the text, it is copy of a reference, so both rtbs are referencing the same Gui object.
    Later when you try to use that reference from the other thread, you get the error because you're trying to reference an object created on the Gui thread.

    The .Text might work, because .Text is a String, and a String is treated as a Value type, not a reference type. So, both rtb's may be referencing the same string, but because it is treated as a value type, and is immutable, it won't cause a object Cross-thread operation error.

    In either case, since it seems like you intend to make a copy of the data, rather than a reference to the data, you shouldn't be doing an assignment like you are. I don't know if the .Rtf has a clone method to create a copy or not. Should look it up, but not curious enough.
    That is the only thing that makes sense, at least it explains the error. The documentation says that .Text and .Rtf are String Properties, though .Text is declared Overrides.

    What I ended up doing was reading the RTF into a string and then assigning the string to both Rtf properties.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,410

    Re: Richtextbox RTF - I learned something

    I wonder if the problem wasn't so much assigning the RTF, but READING the Rtf property...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Richtextbox RTF - I learned something

    The .Rtf and .Text properties are both strings. Why there is a difference seems unknown, but at least it is discussed and there is a workaround should anyone else have the same issue. I'll mark this resolved.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: [RESOLVED] Richtextbox RTF - I learned something

    Form Level Variables work cross thread, but Control Properties don't...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width