Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem regarding two timers in Windows Service
The second TimerLimit is never executed.
I have set up first timer to parse email and i need it to do constantly while the second timer is because i need to be executed after 20 seconds if something happen after having reading emails.
I am a beginner but it seems like there is a conflict.
Is it possible because the first timer is too fast ?
any suggestion to solve this problem ?
Thank you in advance

C#
public partial class GSAService : ServiceBase
    {
        Timer timer;
        Timer timerLimit;
        bool stop = false;
        public GSAService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            stop = false;
            ParseEmail.DeleteFiles();
            timer = new Timer(200);
            timer.Elapsed += new ElapsedEventHandler(ExecuteService);
            timer.Enabled = true;
            timerLimit = new Timer(20000);
            timerLimit.Elapsed += new ElapsedEventHandler(ExecuteServiceLimit);
            timerLimit.Enabled = true;
        }

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

        private void ExecuteService(object source, ElapsedEventArgs e)
        {
            if (stop)
            {
                return;
            }
            try
            {
                if (ParseEmail.isNotUse)
                {
                    ParseEmail.isNotUse = false;
                    ParseEmail.ParseValues();
                }
            }
            catch (Exception exp)
            {
                ParseEmail.isNotUse = true;
                Console.WriteLine("Exception: {0}", exp.ToString());
            }
        }
        private void ExecuteServiceLimit(object source, ElapsedEventArgs e)
        {
            if (stop)
            {
                return;
            }
            try
            {
                    ParseEmail.CheckLimitOrders();
            }
            catch (Exception exp)
            {
                Console.WriteLine("Exception: {0}", exp.ToString());
            }
        }
    }
}
Posted
Updated 30-Mar-12 7:22am
v4
Comments
wizardzz 30-Mar-12 11:36am    
Added code tags.

1 solution

Instead of using Timers, use Threads.

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

For an example, go to this article page:

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=240517[^]

Look for the ReputationScraper, and open the file RepScraperSvc.cs file. It illustrates a scheduling thread. You will need two of them. As far as making threading work for you, google is your friend, and there are a lot of articles here on CP about it.
 
Share this answer
 
v2
Comments
Spada00 30-Mar-12 14:39pm    
Please can you show me how, i am quite new in programming and i have tried but i am not handling this. thank you so much.
#realJSOP 30-Mar-12 16:51pm    
Check my updated answer.
Sergey Alexandrovich Kryukov 30-Mar-12 22:07pm    
Agree, a 5.
--SA

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