Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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


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:
Posted

1 solution

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).

C#
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.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 8-Feb-11 17:39pm    
My 5. Hard to understand logic of some developers: already writing Windows Service, still writing communication is starting thread...
--SA
Yogesh Gulve 9-Feb-11 7:55am    
Hey John Simmons,

Can please explain this code for me step by step? It'll be very beneficial for me.

thanks,
Yogesh
#realJSOP 9-Feb-11 8:06am    
I added comments to the code. Pay special attention to the last paragraph AFTER the code snippet.
Member 10567380 10-Feb-14 0:18am    
if am also trying the similar thing.. if want to change the desktop wallpaper which os services do i have to use
Member 12794907 22-Aug-17 4:57am    
there is one bug in this code.
if (interval >= elapsed) is not correct.

it should if (interval <= elapsed).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900