Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use DispatcherTimer with Windows Service

Why my DispatcherTimer don't work with Windows Service .

The purpose behind that i want use DispatcherTimer for check a windows service License

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

    }


protected override void OnStart(string[] args)
{
    System.Diagnostics.Debugger.Launch();

    if (_helpers.IsValid())
    {
        ConfigureServer();
        StartTimer();
    }
    else
    {
        this.Stop();
    }


}



 private void StartTimer()
        {
            const int time = 10;
            _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)
    {

        EventLog.WriteEntry("test test test ...");
    }
like image 963
Tarek Saied Avatar asked Dec 27 '22 17:12

Tarek Saied


1 Answers

The point of DispatcherTimer is to fire its Tick event on the dispatcher thread. That's important because you can only manipulate Silverlight/WPF UI elements on the dispatcher thread. In a Windows Service, there is no dispatcher thread... there's no UI to manipulate. Why would you want to use a DispatcherTimer instead of (say) System.Timers.Timer?

like image 191
Jon Skeet Avatar answered Jan 15 '23 12:01

Jon Skeet