Windows 服务 +从数据库读取

发布于 2024-12-06 20:23:04 字数 450 浏览 1 评论 0原文

我是 Windows 服务的新手。我需要一个 Windows 服务来从数据库的表中读取条目。我有一个控制台应用程序,我在其中添加新项目 WINDOWS SERVICE。我已经有一个访问数据库的方法和其他方法。我可以在启动时放置一个读取数据库的线程。我把线放在哪里? (我该怎么做)。我在 WINDOWS SERVICE 上的哪里添加这些方法?我有这样的Windows服务:

public Service1()
{
   InitializeComponent();
}

protected override void OnStart(string[] args)
{
   do
   {
      thread.start();
      bool variab = readFromDatabase (Database table);
   }
}

protected override void OnStop()
{
}

I am new To windows service. I need a windows service that reads an entry from a table from database. I have a CONSOLE APP where I add new project WINDOWS SERVICE. I already have a method that access the database, and other methods. I can put a thread on start that reads the database. Where do I put the thread? ( how can I do that). Where on WINDOWS SERVICE I add those methods? I have the Windows Service like this:

public Service1()
{
   InitializeComponent();
}

protected override void OnStart(string[] args)
{
   do
   {
      thread.start();
      bool variab = readFromDatabase (Database table);
   }
}

protected override void OnStop()
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云仙小弟 2024-12-13 20:23:04

我建议您创建一个类,在其中执行您需要的所有操作并在服务中创建:

public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        YourClass cl = new YourClass();
        cl.DoWhatYouNeed(...);       
    }

    protected override void OnStop()
    {
    }

这使您有机会与服务分开运行和测试您的类,也许在调试发布期间。

I suggest that you create a class in which you do everything you need and create in in the service:

public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        YourClass cl = new YourClass();
        cl.DoWhatYouNeed(...);       
    }

    protected override void OnStop()
    {
    }

This gives you opportunity to run and test your class separate from service, maybe during debug release.

〆一缕阳光ご 2024-12-13 20:23:04

对于 Windows 服务,通常会创建一个方法来在单独的线程中执行服务的主循环。否则服务可能会变得无响应。例如,您可以使用名为 MainLoop 的方法来执行服务逻辑。 OnStart 方法仅用于执行初始化任务,例如读取配置值或启动服务的线程。并使用 OnStop 执行清理任务、停止线程等...

Thread _workerThread;
bool _shouldStop;

public Service1()
{
  InitializeComponent();
}    

protected override void OnStart(string[] args)
{
   try{
   _workerThread = new Thread(new ThreadStart(MainLoop));
   _shouldStop = false;
   _workerThread.Start();
   }
   catch{}
}

private void MainLoop()
{
   while (!_shouldStop)
   {
      try{
      //your logic here
      }
      catch{}
   }
}

protected override void OnStop()
{
   _shouldStop = true;
}

With windows services usually a method is created to execute the main loop of the service, in a separated thread. Otherwise the service could become unresponsive. For example, you can have a method called MainLoop to execute the service logic. Use the OnStart method only to do the initializing tasks, such as read configuration values or start the threads of the service. And use the OnStop to executing cleaning tasks, stopping threads, etc...

Thread _workerThread;
bool _shouldStop;

public Service1()
{
  InitializeComponent();
}    

protected override void OnStart(string[] args)
{
   try{
   _workerThread = new Thread(new ThreadStart(MainLoop));
   _shouldStop = false;
   _workerThread.Start();
   }
   catch{}
}

private void MainLoop()
{
   while (!_shouldStop)
   {
      try{
      //your logic here
      }
      catch{}
   }
}

protected override void OnStop()
{
   _shouldStop = true;
}
醉酒的小男人 2024-12-13 20:23:04

您必须将包含数据访问逻辑的代码或类放在 OnStart 方法中

You must put your code or class, which contain data access logic in OnStart method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文