1

This is my code

protected override void OnStart(string[] args)
{
    timAutoUpdate.Enabled = true;
    MessageBox.Show("started");
}

protected override void OnStop()
{
    timAutoUpdate.Enabled = false;
    System.Windows.Forms.MessageBox.Show("Service stopped");
}

private void timAutoUpdate_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Ticked");
}

Its showing message "Ticked" after starting event.If i delete messagebox in Onstart event then Tick event is not working.Its not showing any message.Please specify the reason behind it.I just kept simple message box in Tick event instead of my code.Please tell me the way to achieve it.

 protected override void OnStart(string[] args)
{
    timAutoUpdate.Enabled = true;
}

protected override void OnStop()
{
    timAutoUpdate.Enabled = false;
}

private void timAutoUpdate_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Ticked");
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rakesh Devarasetti
  • 1,409
  • 2
  • 24
  • 42
  • 1
    from Vista up services can't show UI. Don't attempt to show UI from a service. – David Heffernan Jan 11 '12 at 07:18
  • If i didn't show that UI Tick event is not firing.:( – Rakesh Devarasetti Jan 11 '12 at 07:19
  • 1
    You are probably using a UI timer by mistake. Use a timer appropriate for services. – David Heffernan Jan 11 '12 at 07:21
  • 3
    Use either [`System.Timers.Timer`](http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx) or [`System.Threading.Timer`](http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx), **not** `System.Windows.Forms.Timer`. Related question: [Windows service and timer](http://stackoverflow.com/questions/246697/windows-service-and-timer). Obligatory comment: you probably shouldn't be creating a service in the first place. In 83.7% of cases, a standard application that doesn't show a window is a preferable and simpler alternative. – Cody Gray - on strike Jan 11 '12 at 07:30
  • Presently i have changed my "System.windows.Forms.Timer" to "System.Timers.Timer " – Rakesh Devarasetti Jan 11 '12 at 07:31

1 Answers1

0

What timer are you using? is it System.Windows.Forms.Timer?

if this is the case, you need to change it because this timer does not work with Windows service (http://msdn.microsoft.com/en-us/magazine/cc164015.aspx)

Try this code:

    protected override void OnStart(string[] args)
    {
        scheduleTimer = new System.Threading.Timer(new TimerCallback(AutoTick), null, 0, 3000);
        eventLogEmail.WriteEntry("Test");
    }
    private void AutoTick(object state)
    {
        MessageBox.Show("Ticked")
    }
saeedku
  • 11
  • 1