Schedule Event

Hi again...

I need to run this every day at the same time or once a week on a predefined day and hour.

How's it done Been through allot of research but couldn't make it work

This is the event i need to schedule

private void BackUp()

{

Cursor = Cursors.WaitCursor;

SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();

srv.Connect(server, username, password);

SQLDMO.Backup bak = new SQLDMO.BackupClass();

bak.Devices = bak.Files;

bak.Files = BackupFolder + database + ".bak";

bak.Database = database;

bak.SQLBackup(srv);

this.Cursor = Cursors.Default;

}

catch (Exception err)

{

this.Cursor = Cursors.Default;

MessageBox.Show(err.Message, "Erro ao efectuar Backup!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

}

Thanks in advance




Answer this question

Schedule Event

  • Nick Cardullo

    Why not just make it a Console Application and use the Task Scheduler to run the application at the desired time Using the Task Scheduler, you'll have many more options on when to run it, than you could code by hand (given a reasonable amount of time).
  • rwbogosian

    Hi

    try and add this sample code and see if the date appears in in your label, if it does't check if you added the timer control from toolbox


    public string GetTime()
    {
    string TimeInString = "";
    int day = DateTime.Now.Day;
    int month = DateTime.Now.Month;
    int year = DateTime.Now.Year;
    int hour = DateTime.Now.Hour;
    int min = DateTime.Now.Minute;
    int sec = DateTime.Now.Second;
    string AM = "AM";
    string PM = "PM";
    TimeInString = (day < 10)
    "0" + day.ToString() : day.ToString();
    TimeInString +=
    "-" + ((month < 10) "0" + month.ToString() : month.ToString());
    TimeInString +=
    "-" + ((year < 10) "0" + year.ToString() : year.ToString());
    TimeInString +=
    " " + ((hour < 10) "0" + hour.ToString() : hour.ToString());
    TimeInString +=
    ":" + ((min < 10) "0" + min.ToString() : min.ToString());
    TimeInString +=
    ":" + ((sec < 10) "0" + sec.ToString() : sec.ToString());
    TimeInString +=
    " " + ((hour < 12) "" + AM : PM);
    return TimeInString;
    }
    private void Clock_Tick(object sender, EventArgs e)
    {
    if (sender == timer1)
    {
    lbTime.Text = GetTime();
    //lbTime is the label

    }
    }

    hope this helps



  • ble0t

    I suppose this is not what you want:

    PedroSimao wrote:

    int minute = DateTime.Now.Hour;

    and

    int minute = DateTime.Now.Minute;

    would be more appropriate.


  • Ghoort

    Hi

    You can use a timer control in your toolbox to run BackUp() at the specified time frames you set like:-

    in the formName.Designer.cs you will have:-


    //

    // timer1

    //

    this.timer1.Enabled = true;
    this.timer1.Interval = 1000;
    this.timer1.Tick += new System.EventHandler(this.Clock_Tick);

    your code will look like :-


    private void Clock_Tick(object sender, EventArgs e)
    {
    if (sender == timer1)
    {
    int hour = DateTime.Now.Hour;
    int minute = DateTime.Now.Hour;
    int second = DateTime.Now.Second;
    if (hour == 11 && minute == 30 && second == 0)//Run every day at 11:30:00 AM

    {
    BackUp();
    }
    //OR

    int day_of_week = DateTime.Now.DayOfWeek;
    if(day_of_week = 3 && hour == 11 && minute == 30 && second == 0)//Run every Wednsday at 11:30:00 AM

    {
    BackUp();
    }
    }
    }

    Hope this helps



  • Sabrecat

    that was the thing :)

    thanks to all!



  • UK_12

    Sander_G wrote:

    I suppose this is not what you want:

    PedroSimao wrote:

    int minute = DateTime.Now.Hour;

    and

    int minute = DateTime.Now.Minute;

    would be more appropriate.

    Your are right, thanks



  • chubbysilk

    thanks Sibusiso

    That's right about it!



  • Julian V

    I'm missing something here...

    here's what i'm doing:

    DateTime schedHour = DateTime.Parse(lblSchedule.Text.ToString());

    h = Convert.ToInt32(schedHour.Hour);

    m = Convert.ToInt32(schedHour.Minute);

    System.Windows.Forms.Timer Clock;

    Clock = new System.Windows.Forms.Timer();

    Clock.Tick += new System.EventHandler(this.Clock_Tick);

    Clock.Interval = 1000;

    Clock.Start();

    private void Clock_Tick(object sender, EventArgs e)

    {

    if (sender == Clock)

    {

    int hour = DateTime.Now.Hour;

    int minute = DateTime.Now.Hour;

    int second = DateTime.Now.Second;

    if (hour == h && minute == m && second == 0)

    {

    BackUp();

    }

    }

    }

    ...nothing happens, Backup() doesn't start....

    Thanks again



  • Schedule Event