Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to run C#.net application automatically in two min interval.My code is given below

What I have tried:

<pre lang="c#">
//Form1.cs
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;

namespace AutoBackup
{
    public partial class Form1 : Form
    {
        Backup objBackup = new Backup();
        NotifyIcon notifyIcon = new NotifyIcon();
        RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true);
        public Form1()
        {
            InitializeComponent();
            Thread splashthread = new Thread(new ThreadStart(objBackup.backup));
            splashthread.IsBackground = true;
           

            try
            {
                //objBackup.IPAddress();
                System.Timers.Timer BackUpTimer = new System.Timers.Timer();
                BackUpTimer.Interval = Convert.ToInt32(2000);
                
                //replicationTimer.Interval = 300000;
                BackUpTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
                BackUpTimer.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            
        }

    }
}




//Backup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;

namespace AutoBackup
{
    public class Backup
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);      
        public void backup()
        {
            using (XmlWriter writer = XmlWriter.Create(path+"\\BackupDetails.xml"))
            {
                writer.WriteStartElement("Backups");
                
                writer.WriteElementString("Time", DateTime.Now.ToString());
                
                writer.WriteEndElement();
                writer.Flush();
            }
        }
    }
}


Plz help..
Posted
Updated 12-Feb-20 22:08pm

Change this:
C#
BackUpTimer.Interval = Convert.ToInt32(2000);
To this:
C#
BackUpTimer.Interval = Convert.ToInt32(2000 * 60);
 
Share this answer
 
Comments
Member 12386982 13-Feb-20 2:22am    
not working
phil.o 13-Feb-20 3:03am    
What does 'not working' mean? Interval is expressed as a number of millisecconds, so when you specify 2000ms it means 2s. And nothing is done on tick of the timer, or you didn't show it.
Please, help us to help you and provide much more sensible informations than a generic 'not working'.
OriginalGriff 13-Feb-20 3:32am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
phil.o 13-Feb-20 3:05am    
Convert.ToInt32(2000 * 60)
Hum... :)
OriginalGriff 13-Feb-20 3:14am    
Yeah ... I think the OP is a little out of his depth ...
It might also be a good idea to actually call the backup routine in the
MyTimer_Elapsed
event handler.

e.g.
C#
Backup myBackup = new Backup();
myBackup.backup();

Your current event handler does nothing at the moment!
 
Share this answer
 

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