【问题标题】:how to use windows service in windows form application [closed]如何在 Windows 窗体应用程序中使用 Windows 服务 [关闭]
【发布时间】:2013-06-10 09:57:46
【问题描述】:

如何在 windows 窗体应用程序中使用 windows 服务? 我有数据库表,包括黄金、白银等。价格。 这些可以显示在 Windows 窗体中。

我想在 Windows 窗体中定期更新这些东西(例如:每 10 分钟,需要更新)。 有没有可能的方法?

【问题讨论】:

标签: c# windows-services


【解决方案1】:

您可以使用 Timer 定期更新数据库

Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
    timer.Interval = (10) * (1);             // Timer will tick evert 10 seconds
    timer.Enabled = true;                       // Enable the timer
    timer.Start();

void timer_Tick(object sender, EventArgs e)
{
    //Put your technique for updating database here
}

你可以像这样调用服务

using System.ServiceProcess;
ServiceController sc = new ServiceController("My service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
   sc.Start();
}

【讨论】:

  • 我知道 Windows 服务中的这些定期更新。是否可以在 Windows 窗体中更新?
  • 如前所述,使用定时器进行周期性更新。
【解决方案2】:

使用Forms timer

如果更新时间超过几秒钟,请在后台工作人员中进行工作。如果您使用后台工作程序,我会将其包装在代码中以防止多个同时执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多