none
Anyone know how to create auto save function? RRS feed

  • Question

  • Im currently developing GUI to save my data every 1 hour without using button save function.

    Anyone know how to do it?

    Wednesday, April 25, 2018 8:08 PM

All replies

  • It seems so easy to do that I probably misunderstand the question. Can you clarify? What do you need help with specifically?


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, April 25, 2018 8:17 PM
  • Hi,

    Your question is not clear. You are creating a graphical user interface for an autosave function. I interpret this as een an option to set autosave on or off.

    Use the settings options in your application to create the setting as an bool.

    Create an timer function with a callback and, depending on the autosave option setting start the timer with the time.

    As the timer expires the event will be launched and your delegate will be called. In the delegate function: do your datasave.

    restart the timer.

    If the option is switched off by the user, the value change can launch an event as well. In that delegate you can stop the timer (or restart it)

    You can check the initial state at formload and start the timer.

    Hope this helps you, if so, please mark as answer.

    Best regards,

    Wednesday, April 25, 2018 8:39 PM
  • I agree with KKR.

    Documentation is here.

    In the following snippet I setup a timer with a delegate, Dispatcher which performs work, disposes of the timer then initialize the timer again. Note the line mServiceTimer.Change, this is were you setup the duration for the timer. 

    Granted this comes from a Window service but the same can be done in a form or a class.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 25, 2018 9:28 PM
    Moderator
  • Example of code to save text from textbox into notepad.

    Thursday, April 26, 2018 7:36 AM
  • Hello LAW0521,

    The below example set interval as 5 seconds and you should modify it to 1*60*60*1000(1 hour).

       private void button1_Click(object sender, EventArgs e)
            {
                System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(test),null,0,5000);            
            }
    
            private void test(object state)
            {
                using (StreamWriter sw = File.AppendText("t1.txt")) {                
                    sw.WriteLine(textBox1.Text);
                }
            }

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 3, 2018 7:55 AM
    Moderator