使用DispatcherTimer与Windows服务 [英] Use DispatcherTimer with Windows Service

查看:129
本文介绍了使用DispatcherTimer与Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的DispatcherTimer不与Windows服务工作。



背后的目的,我想使用DispatcherTimer用于检查一个窗口服务许可证

 公共OIMService()
{
的InitializeComponent();
_dispatcherTimer =新DispatcherTimer();
如果(System.Diagnostics.EventLog.SourceExists(OIM_Log)!)
{
EventLog.CreateEventSource(OIM_Log,OIMLog);
}
EventLog.Source =OIM_Log;
EventLog.Log =OIMLog;
_helpers =新ValidationHelpers();
startTimer所();

}


保护覆盖无效的OnStart(字串[] args)
{
System.Diagnostics.Debugger.Launch();

如果(_helpers.IsValid())
{
ConfigureServer();
startTimer所();
}
,否则
{
this.Stop();
}


}



私人无效startTimer所()
{
const int的时间= 10;
_dispatcherTimer.Tick + =新的EventHandler(DispatcherTimerTick);
_dispatcherTimer.Interval =新时间跨度(0,0,Convert.ToInt32(时间));
_dispatcherTimer.IsEnabled = TRUE;
_dispatcherTimer.Start();
}
私人无效DispatcherTimerTick(对象发件人,EventArgs五)
{

EventLog.WriteEntry(测试测试测试...);
}


解决方案

的点 DispatcherTimer 是消防调度线程上的勾选事件。这是因为你只能操纵调度线程上的Silverlight / WPF UI元素是很重要的。在Windows服务中,有的没有调度线程进行...有没有UI操控。你为什么要使用的 DispatcherTimer 而不是(说)的 System.Timers.Timer


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 ...");
    }

解决方案

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?

这篇关于使用DispatcherTimer与Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆