Click here to Skip to main content
11,644,151 members (73,398 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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

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 30-Mar-12 5:04am
Spada00343
Edited 30-Mar-12 7:22am
v4
Comments
wizardzz at 30-Mar-12 11:36am
   
Added code tags.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Spada00 at 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.
John Simmons / outlaw programmer at 30-Mar-12 16:51pm
   
Check my updated answer.
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 570
1 jyo.net 484
2 DamithSL 318
3 Afzaal Ahmad Zeeshan 253
4 CPallini 222
0 OriginalGriff 795
1 DamithSL 611
2 Mika Wendelius 494
3 jyo.net 484
4 Afzaal Ahmad Zeeshan 404


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 30 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100