在后台连续运行Windows服务 [英] Running windows service continuously in background

查看:387
本文介绍了在后台连续运行Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在尝试编写一项win服务来生成一些pdf文件并发送邮件.为此,我正在检查数据库中的状态,如果电子邮件状态为0,则发送邮件,如果报告状态为0,则生成报告.我希望这会在Win服务启动后立即发生.为此,我有一个方法"GetEmployees".

现在我的问题是如何知道获胜服务已启动以及OnStart事件发生后如何调用GetEmployees?一旦启动获胜服务,则此过程将在后台连续运行.我召集OnStart事件之后需要代码.如果我在OnStart上调用GetEmployees方法,则此事件未完成,因为一旦过程完成,我将再次调用GetEmployees方法.
对于例如

Hello Friends,

I am trying to write one win service to generate some pdf''s and sending mails. For this I am checking status in database, if email status is 0 then send mail and if report status is 0 then generate reports. I want this to be happen as soon as win service started. For this I have one method "GetEmployees".

Now my question is how to know win service started and how do I call GetEmployees after OnStart event? Once the win service is started then this process will run continuously in background. I rally need the code for after OnStart event. If I call GetEmployees method on OnStart then this event is not completing because I am calling GetEmployees method again once the process is completed.

For e.g

''''''Here starts the win service
Protected Overrides Sub OnStart(ByVal args() As String)

    getEmployees()

End Sub


Private Sub getEmployees()
 
    ''Process Start
       ''PDF creation and email sending process here

    ''Process End

    ''again calling getEmployees method
    getEmployees()

End Sub



我希望你们能回答我的问题.请告诉我更好的方法.这将对我非常有帮助,因为我是第一次编写胜利服务. :)

谢谢,
Yogesh
:cool:



I hope you guys get my question. Please tell me the better way to do this. That will be very helpful for me because I am writing win service for the first time. :)

Thanks,
Yogesh
:cool:

推荐答案

您需要使用多线程处理-诸如此类(以下内容在C#中使用,但是使用以下方法之一将其转换为VB应该很简单:许多在线免费翻译器.)

You need to use multithreading - something like this (the following is in C#, but it should be a simple matter to translate to VB using one of the many online free translaters).

public class MyService...
{
    Thread m_thread = null;
 
    public void OnStart()
    {
        // instantiate the thread
        m_thread = new Thread(new ThreadStart(ThreadProc));
        // start the thread
        m_thread.Start();
    }
 
    public void ThreadProc()
    {
        // we're going to wait 5 minutes between calls to GetEmployees, so 
        // set the interval to 300000 milliseconds 
        // (1000 milliseconds = 1 second, 5 * 60 * 1000 = 300000)
        int interval = 300000; // 5 minutes    
        // this variable tracks how many milliseconds have gone by since 
        // the last call to GetEmployees. Set it to zero to indicate we're 
        // starting fresh
        int elapsed  = 0;
        // because we don't want to use 100% of the CPU, we will be 
        // sleeping for 1 second between checks to see if it's time to 
        // call GetEmployees
        int waitTime = 1000; // 1 second
        try
        {
            // do this loop forever (or until the service is stopped)
            while (true)
            {
                // if enough time has passed
                if (interval >= elapsed)
                {
                    // reset how much time has passed to zero
                    elapsed = 0;
                    // call GetEmployees
                    GetEmployees();
                }
                // Sleep for 1 second
                Thread.Sleep(waitTime);
                // indicate that 1 additional second has passed
                elapsed += waitTime;
            }
        }
        catch (ThreadAbortException)
        {
            // we want to eat the excetion because we don't care if the 
            // thread has aborted since we probably did it on purpose by 
            // stopping the service.
        }
    }
}



编辑====================

我根据您的要求对代码进行了注释.这是一个非常基本的坐骑旋转线程循环.这是我将要提供的详细解释.如果您仍然不了解代码在做什么,我建议您去学校学习(或重新学习)如何编程.



EDIT =====================

I commented the code at your request. This is an extremely basic sit-and-spin thread loop. This is as detailed of an explanationas I''m going to provide. If tyou STILL don''t understand what the code is doing, I suggest that you go to school and learn (or re-learn) how to program.


这篇关于在后台连续运行Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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