Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am designing the windows service in c#. I have set time interval of 1 minute.
How to check the previous process is completed on next timer start to prevent multiple entry.
I Want to check the previous service process if that process is not completed i want to skip this current process.

What I have tried:

i have set
public bool isComplete = true;
on timer i have checked

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if (isComplete == true)
    {
        try
        {
            isComplete = false;
            eventLog1.WriteEntry("Service Initial Started at " + DateTime.Now.ToString() + Environment.NewLine);
            NexGenServiceRun();
            eventLog1.WriteEntry("Service Final Completed at " + DateTime.Now.ToString() + Environment.NewLine);
            isComplete = true;
        }
        catch
        {
            isComplete = true;
        }

    }
    else
    {
        eventLog1.WriteEntry("Service not started, Previous service is not completed " + Environment.NewLine );
    }
}
Posted
Updated 24-Jan-17 20:16pm

1 solution

Generally, your idea is right. The only issue would be a possible race condition on your isComplete variable. Instead of doing the checks/assign as you have done, use the Interlocked.Exchange(T) Method (T, T) (System.Threading)[^] to check the existing value, and to set it to false, in an atomic operation.
 
Share this answer
 

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