Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a xml file which i have to load in richtextbox in wpf. if some changes occure in txt file then it will save in different path automatically after a time interval of 1 min. Let the 1st changes happens in 10:11 then all the changes made up to 10:12 will save.File creation time will be 10:12 then again the richtextbox will be modified at 10:50 then the file modified time will be 10:51.The loading and the saving path is different.

private void startSaveTimer()
{
Timer saveTimer = new Timer(60000);
saveTimer.Elapsed += saveTimer_Elapsed;
saveTimer.Start();
}

private void saveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
string filepath = @"C:\mydoc.txt";//file save path
if (File.Exists(filepath))
{
FileStream file = new FileStream(filepath, FileMode.Open);
//geting the content from file and compare with richtextbox data
//if not same then saving it again
}
else
{
//save file
}
}

private void richText_TextChanged(object sender, TextChangedEventArgs e)
{
startSaveTimer();
}
but this code called in each single changed so
FileStream file = new FileStream(filepath, FileMode.Open); is showing error as used by different user. try to keep lock still got the same error.
Thanks in advance
Posted
Updated 14-Jun-15 6:19am
v4
Comments
CHill60 13-Jun-15 9:48am    
What have you tried? What is not working in your code?
Starlene 14-Jun-15 12:14pm    
added the code and the error

1 solution

Hi,

You can use something like a DispatcherTimer and set its interval to 60000 milliseconds, or, if not available, 1000 milliseconds and then count 60 occurrences of the method.

Then you can do this way:

1. Use the event TextChanged of the RichTextBox control and set a bool flag (called, for istance, Saved) to false to indicate that it is not saved and then start the timer if not already started.
2. In the timer handler, if set to 60000 millis, save the richtextbox and disable the timer. This way the document does not get saved if no modifies are made in the next minutes.

Here is the code:
C#
FileStream file = new FileStream(filepath, FileMode.Open);

Timer saveTimer = new Timer(60000);
private void startSaveTimer()
{

saveTimer.Elapsed += saveTimer_Elapsed;
saveTimer.Start();
}
private bool saved=true;
private void saveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
string filepath = @"C:\mydoc.txt";//file save path
if (!saved)
{
//save file
saved=true;
timer.enabled=false;
}
}

private void richText_TextChanged(object sender, TextChangedEventArgs e)
{
saved=false
if(!timer.enabled)
timer.enabled=true;
}


I hope this helps...

PS: I advise you to get the current RichTextBox content and pass it in another thread in order not to block the UI while you are saving... This can save you some troubles :)

LG
 
Share this answer
 
v2
Comments
Starlene 15-Jun-15 2:40am    
can u check the code
LLLLGGGG 15-Jun-15 3:43am    
Mmhh. I think that it is not necessary to compare the richtextbox data with the content of the file. In that case, I'd use a boolean flag: when you load the content of the textbox, set it to true because no changes have been made. When richtextbox.TextChanged is fired, set it to false and start the timer. When the timer elapses, save the content of richtextbox and set the boolean to true and stop the timer. I have just updated the answer with the code. You can notice that the filepath variable is still inside a method. Declare it as a global variable. I'm sorry I cannot do more now but I am writing with a tablet and codeproject does not seem to be mobile friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900