70002795 AutoSave example

Article 70002795
Type HowTo
Product Engine
Date Added 12/5/2025 12:00:00 AM
Fixed 11.5.03 (12/5/2025 12:00:00 AM)
Submitted by prizmayazilim

Summary

An example that save the drawing periodically inside a timer into a temporary folder in order not loose your work for an unexpected reason

Solution

Suppose you have a form application with a vdFrameControl

private vdDocument doc { get { return vdFramedControl.BaseControl.ActiveDocument; } }

System.Timers.Timer autosavetimer = new System.Timers.Timer(1000);//create a timer with 1 sec interval for test

private void Form1_Load(object sender, EventArgs e)
{
    autosavetimer.Elapsed += Autosavetimer_Elapsed;
    autosavetimer.Start();  

}

        private void Autosavetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            lock (doc)//lock with doc to make the procedure multi thread safe
            {
                if (!doc.IsModified) return;//if no change in the drawing do nothing
                string oldname = doc.FileName;//keep the existing filename of the document to restore it after save command
                string fname = doc.FileName;
                if (fname == "") fname = System.DateTime.Today.ToString("yyyy-MM-dd") + ".vdcl";
                string autosavepath = Application.LocalUserAppDataPath + @"\AutoSave\";//a forder to keep the auto saved drawings So we can delete them later
                System.IO.Directory.CreateDirectory(autosavepath);
                fname = autosavepath + System.IO.Path.GetFileName(fname) + "_tmp.vdcl";
                doc.SaveAs(fname);
                doc.FileName = oldname;
                doc.IsModified = false;//set the modify to false since we have already save the drawing in a safe place of our disk
            }
        }

Send comments on this topic.