-4

Possible Duplicate:
Use DispatcherTimer with Windows Service

i want to check for a license every day in my windows Service i try use DispatcherTimer but not working this is what I tried to do

 public OIMService()
    {
        InitializeComponent();
        _dispatcherTimer = new DispatcherTimer();
        this.ServiceName = "OIMService";
        if (!System.Diagnostics.EventLog.SourceExists("OIM_Log"))
        {
            EventLog.CreateEventSource("OIM_Log", "OIMLog");
        }
        EventLog.Source = "OIM_Log";
        EventLog.Log = "OIMLog";
        _sc = new ServiceController("OIMService");
        _helpers = new ValidationHelpers();
        StartTimer();

    }

  private void StartTimer()
        {
            _dispatcherTimer.Tick += new EventHandler(DispatcherTimerTick);
            _dispatcherTimer.Interval = new TimeSpan(0, 0, Convert.ToInt32(time));
            _dispatcherTimer.IsEnabled = true;
            _dispatcherTimer.Start();
        }

    private void DispatcherTimerTick(object sender, EventArgs e)
    {
        var helpers  = new ValidationHelpers();
        if (!helpers.IsValid())
            this.Stop();

    }
Community
  • 1
  • 1
Tarek Saied
  • 6,482
  • 20
  • 67
  • 111

1 Answers1

1

You can use a new Thread like this:

    bool cancelJob = false;

    ThreadStart ts = new System.Threading.ThreadStart(CheckLicence);
    Thread thMain = new System.Threading.Thread(ts);
    thMain.Start();


    void CheckLicence()
    {
        //you can use cancelJob to break the thread...
        while (!this.cancelJob)
        {
            //TODO: code to check your licence...

            //sleep for 1 hour...
            System.Threading.Thread.Sleep(3600000);
        }
    }

When you want to stop the service make sure you terminate the thread also, like this:

        thMain.Abort();
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46