Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Preventing filedestruction after saving richtextbox contents at program end

Preventing filedestruction after saving richtextbox contents at program end

Scheduled Pinned Locked Moved C#
question
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fracalifa
    wrote on last edited by
    #1

    Hi all, the last operation of my program is to store the contents of a richtextbox to a file. Often the contents of the file will be destroyed. Saving by hand (button) never destroys the contents. So it seems that the saving task (asynchronious ???) will be finished too soon at the end of my program. I inserted a sleeptime to prevent the destruction an to give the savetask the time it needs but that's no solution steadily. How can I detect, the end of the savetask of the richtextbox before finishing the program ? tnx Frank

    W D 2 Replies Last reply
    0
    • F fracalifa

      Hi all, the last operation of my program is to store the contents of a richtextbox to a file. Often the contents of the file will be destroyed. Saving by hand (button) never destroys the contents. So it seems that the saving task (asynchronious ???) will be finished too soon at the end of my program. I inserted a sleeptime to prevent the destruction an to give the savetask the time it needs but that's no solution steadily. How can I detect, the end of the savetask of the richtextbox before finishing the program ? tnx Frank

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Since the saving can be performed in numerous ways, it's impossible to pinpoint the problem without seeing the code. One reason could be that you don't close the stream, but as said, please post the code.

      The need to optimize rises from a bad design.My articles[^]

      F 1 Reply Last reply
      0
      • F fracalifa

        Hi all, the last operation of my program is to store the contents of a richtextbox to a file. Often the contents of the file will be destroyed. Saving by hand (button) never destroys the contents. So it seems that the saving task (asynchronious ???) will be finished too soon at the end of my program. I inserted a sleeptime to prevent the destruction an to give the savetask the time it needs but that's no solution steadily. How can I detect, the end of the savetask of the richtextbox before finishing the program ? tnx Frank

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Works fine for me. Where are you calling the SaveFile method from (which event - FormClosing?). Which overload of SaveFile are you using?

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        1 Reply Last reply
        0
        • W Wendelius

          Since the saving can be performed in numerous ways, it's impossible to pinpoint the problem without seeing the code. One reason could be that you don't close the stream, but as said, please post the code.

          The need to optimize rises from a bad design.My articles[^]

          F Offline
          F Offline
          fracalifa
          wrote on last edited by
          #4

          I am simply using the safefile method of the richtextbox control, that's all.

          rtxt.SaveFile(Application.StartupPath + "\\log.rtf");

          and rtxt is from type System.Windows.Forms.RichTextBox I did'nt found a method for the RTB like .flush or .close to finish the stream securely. tnx

          D W 2 Replies Last reply
          0
          • F fracalifa

            I am simply using the safefile method of the richtextbox control, that's all.

            rtxt.SaveFile(Application.StartupPath + "\\log.rtf");

            and rtxt is from type System.Windows.Forms.RichTextBox I did'nt found a method for the RTB like .flush or .close to finish the stream securely. tnx

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Try this in your FormClosing event handler:

            using (System.IO.Stream stream = new System.IO.FileStream(
            Application.StartupPath + @"\log.rtf", System.IO.FileMode.Create))
            {
            rtxt.SaveFile(stream, RichTextBoxStreamType.RichText);
            }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            F 1 Reply Last reply
            0
            • F fracalifa

              I am simply using the safefile method of the richtextbox control, that's all.

              rtxt.SaveFile(Application.StartupPath + "\\log.rtf");

              and rtxt is from type System.Windows.Forms.RichTextBox I did'nt found a method for the RTB like .flush or .close to finish the stream securely. tnx

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              That should work fine but at which point are you calling this? Is the contents of rtxt possibly destroyed?

              The need to optimize rises from a bad design.My articles[^]

              F 1 Reply Last reply
              0
              • W Wendelius

                That should work fine but at which point are you calling this? Is the contents of rtxt possibly destroyed?

                The need to optimize rises from a bad design.My articles[^]

                F Offline
                F Offline
                fracalifa
                wrote on last edited by
                #7

                I call the method as last instruction in the method main[]. tnx frank

                W 1 Reply Last reply
                0
                • D DaveyM69

                  Try this in your FormClosing event handler:

                  using (System.IO.Stream stream = new System.IO.FileStream(
                  Application.StartupPath + @"\log.rtf", System.IO.FileMode.Create))
                  {
                  rtxt.SaveFile(stream, RichTextBoxStreamType.RichText);
                  }

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  F Offline
                  F Offline
                  fracalifa
                  wrote on last edited by
                  #8

                  Thank you, that sounds good, I will try this. tnx Frank

                  1 Reply Last reply
                  0
                  • F fracalifa

                    I call the method as last instruction in the method main[]. tnx frank

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    At that point you won't have anything to save. Use FormClosed event instead and save the contents of the control in that event.

                    The need to optimize rises from a bad design.My articles[^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups