Preventing filedestruction after saving richtextbox contents at program end
-
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
-
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
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[^]
-
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
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) -
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[^]
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
-
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
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) -
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
-
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[^]
-
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)