Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

i have problem about my code.
When i run this code hourly reporting method is working fine however
"
C#
StartTimer(new TimeSpan(08, 39, 0), new TimeSpan(24, 0, 0), ProcessViewType.SingleRun);
StartTimer(new TimeSpan(08,40 ,0), new TimeSpan(24, 0, 0), ProcessViewType.ReportToBranch);
"

this part is not work.
I try debuging mode when the hourly timer is work sineglerun and the other timer is not work.How ever i delete the hourly event the others works fine.I think the hourly event Suppress the other events. How can i fix?

why? and how can i fix this problem?

C#
timingToolStripMenuItem.Enabled = false;
            StartTimer(new TimeSpan(15, 47, 0), new TimeSpan(24, 0, 0), ProcessViewType.ReportToBranch, true);
            StartTimer(new TimeSpan(12, 44, 0), new TimeSpan(24, 0, 0), ProcessViewType.SingleRun,false);
            StartTimer(new TimeSpan(12, 46, 0), new TimeSpan(24, 0, 0), ProcessViewType.ReportToBranch,false);


        }

        protected void StartTimer(TimeSpan scheduledRunTime, TimeSpan timeBetweenEachRun,ProcessViewType pc,bool Hourly)
        {
            // Initialize timer
            double current = DateTime.Now.TimeOfDay.TotalMilliseconds;
            double scheduledTime = scheduledRunTime.TotalMilliseconds;
            double intervalPeriod = timeBetweenEachRun.TotalMilliseconds;
            // calculates the first execution of the method, either its today at the scheduled time or tomorrow (if scheduled time has already occured today)
            double firstExecution = current > scheduledTime ? intervalPeriod + (intervalPeriod - current) : scheduledTime - current;
            System.Threading.TimerCallback callback = null;
            // create callback - this is the method that is called on every interval
            if (pc == ProcessViewType.ReportToBranch && Hourly == false)
            {
                callback = new System.Threading.TimerCallback(runReport);
            }
            else if (pc == ProcessViewType.SingleRun && Hourly == false)
            {
                callback = new System.Threading.TimerCallback(runSingleRun);
            }
            else if (pc == ProcessViewType.ReportToBranch && Hourly == true)
            {
                callback = new System.Threading.TimerCallback(runReport);
                firstExecution = 1;
                intervalPeriod=TimeSpan.FromMinutes(3).TotalMilliseconds;
            }

            // create timer
            _timer = new System.Threading.Timer(callback, null, Convert.ToInt32(firstExecution), Convert.ToInt32(intervalPeriod));


What I have tried:

changing timer line
first run the dayly timer the methods are called.
Posted
Updated 6-Mar-17 23:25pm
v2
Comments
Richard MacCutchan 6-Mar-17 3:23am    
The Milliseconds values are integers so why are you converting them? Do not use double types except in complex mathematics/statistics.

1 solution

When you get stuck like this there are two options:
A. Use the debugging features of Visual Studio: Set breakpoints and watch if you code is behaving as desired.

B. Break out the problem into a prototype solution as follows:
1. Start a new project, put a button on a form and set up a timer in the code-behind page.
2. Run to confirm that it works.
3. Expand the functionality one step.
4. Repeat steps 2. & 3. until full functionality is working.
5. Fix code in app.

I used Step B. above a couple of hours ago to resolve a complex xml encoding format issue that I was having...
 
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