Results 1 to 11 of 11

Thread: [RESOLVED] auto-save user's typing

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Resolved [RESOLVED] auto-save user's typing

    As I type a lot of text here, I see a yellow label flash "auto-save" periodically. Can I do this on a DevExpress memoedit in Windows my application?
    Last edited by MMock; Nov 21st, 2017 at 12:01 PM. Reason: Said it was a Windows application...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: auto-save user's typing

    The way it works on this site is that the text is saved to browser storage (I think a cookie), and when you reload the same thread it auto-fills using the saved text.

    Doing it in Windows application can be done in pretty much the same way: save the text occasionally (perhaps on the _Changed event, or using a timer, or a mixture of both, etc) to some kind of storage (perhaps a database, or text file, etc). When the application/window loads, retrieve the stored text and put it into the control.

    If you want to treat it as temporary, either have some way of deleting the stored value, or don't even store it permanently (just use a variable) if you want it only while the app is still running.

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: auto-save user's typing

    Yes, it is coming along nicely. I am using a background worker triggered by a timer, because it is file i/o and I didn't know if that would take away from the user typing if I did it in the same thread.

    Here it is so far:
    Code:
            
    #region BackgroudWorkerSavesResolution
    
            private void timerSaveResolution_Tick(object sender, EventArgs e)
            {
                timerSaveResolution.Enabled = false;
                lbResolutionAutoSaved.Visible = true;
                bkgwrkSaveResolution.RunWorkerAsync(meResolution);
                timerSaveResolution.Enabled = true;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void bkgwrkSaveResolution_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                MemoEdit meTomsLongText = (MemoEdit)e.Argument;
    
                // Sleep...because, why?
                System.Threading.Thread.Sleep(1000);
    
                // Write what's in resolution so far
                string fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SavedResolution.txt");
                TextWriter twResolution = new StreamWriter(fileName);
                twResolution.Write(meTomsLongText.Text);
                twResolution.Close();
            }
    
            private void bkgwrkSaveResolution_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
            {
                lbResolutionAutoSaved.Visible = false;
    
            }
    
            #endregion
    lbResolutionAutoSaved is a yellow label that goes on and off (might've stolen that from somewhere ). Now all I have to do is turn off the timer when the user isn't actually in the memoedit anymore because no sense saving something that's not changing, and detecting if the file exists when the form loads so I can ask the user if he wants to recover. And I have to do this by "job". So if the user opens the same job he was in during the crash, ask him, but if he opens a different job, start a new file with a unique name. And maybe ask him if he wants to go back to that previous job and recover it. And delete the file once the data's saved. Am I forgetting anything???
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: auto-save user's typing

    This part can be simplified:
    Quote Originally Posted by MMock View Post
    Now all I have to do is turn off the timer when the user isn't actually in the memoedit anymore because no sense saving something that's not changing,
    Rather than having the timer running constantly, you can enable it only when a key is pressed (resetting it each time a key is pressed) and disable it if you save.


    At the moment you aren't keeping track of which 'job' it applies to, but I assume you are already planning to do that.

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: auto-save user's typing

    Yes, I am adding jobRow.Control to the name of the file.
    Thanks for the tip on the timer...I would think that's an awful lot of times turning the timer on. Would I maybe turn it on when the MemoEdit gets the focus and turn it off when it loses focus? I suppose the user could leave it focused and walk away but is there any harm in that? If he went home for the day, I would time out the whole form after 15 minutes of inactivity.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: auto-save user's typing

    If you set it up so that the timer is only enabled by a keypress in the desired control, the timer will only act if the user has typed something since text was last stored.

    While resetting the timer there (so that the interval is from their last keypress, and not during a series of keys) is toggling the state of the timer fairly regularly, it isn't a big deal... and it means that periods of inactivity from the user also mean no activity by the code.

  7. #7

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: auto-save user's typing

    If I implement your solution, do I not need a backgroundworker? Because I am using one. And when I tried typing, which is pretty rapid-fire, I got an exception "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently". He is writing out my data but I've got a backlog of keystrokes to be processed. The exception is noted below.
    Code:
            private void timerSaveResolution_Tick(object sender, EventArgs e)
            {
                timerSaveResolution.Enabled = false;
                lbResolutionAutoSaved.Visible = true;
                // Exceptioned...
                bkgwrkSaveResolution.RunWorkerAsync(meResolution);
            }
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: auto-save user's typing

    A backgroundworker is a good idea, because it reduces the chances of the saving interfering with the users actions.

    It sounds like you aren't resetting the timer in the key event, and that is an important step (so it doesn't try to save too frequently).

    It would also be worth adding a check to the _Tick event, so that if bkgwrkSaveResolution is busy then the current actions do not take place (so it will be re-tried after the next interval).

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: auto-save user's typing

    Thank you. My last issue is the blinking of auto-save. Your solution is probably better, to write out the data based on the text changing versus every 10 seconds, but now that I am saving changing data way more often than every 10 seconds, my label blinks way too often. Should it be on some kind of a timer?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: auto-save user's typing

    It sounds to me as if you've got the timer interval too low, I'd recommend that you set it to 3 seconds or more... and if it is being reset by keypresses, that will mean it shouldn't fire more than once in a typical 10 seconds (generally it should only run after a user pauses for a while, so save much less often than 10s).

    If that doesn't sort it out it would be a good idea to show the timer and key events, and let us know the timer interval.

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: auto-save user's typing

    Nope, I'm good now! Thanks!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

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



Click Here to Expand Forum to Full Width