【问题标题】:how to check for a license every day in Windows Service [duplicate]如何每天在 Windows 服务中检查许可证 [重复]
【发布时间】:2012-09-16 18:44:16
【问题描述】:

可能重复:
Use DispatcherTimer with Windows Service

我想每天在我的 Windows 服务中检查许可证 我尝试使用 DispatcherTimer 但不工作 这就是我试图做的

 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();

    }

【问题讨论】:

  • 不是重复这个问题是关于我如何检查 Windows 服务中的许可证而不是关于 DespatcherTimer
  • @tito11,它要么完全重复,要么完全缺少示例代码(我看到零行代码以某种方式与“许可证...”相关)

标签: c# windows-services


【解决方案1】:

你可以像这样使用一个新的线程:

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

当您想停止服务时,请确保您也终止线程,如下所示:

        thMain.Abort();

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2013-01-02
    • 2010-10-04
    相关资源
    最近更新 更多