Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Timer class Change(dueDate, periord)

Tags:

c#

i have go through the msdn library about this timer class change function ,

http://msdn.microsoft.com/en-us/library/yz1c7148.aspx

public bool Change( int dueTime, int period )

But i do not understand what is the period parameter for.

i also try to create a sample to see what it for but seems like it is doing nothing

Timer JobTime = new Timer(timer =>
        {
            try
            {
                WriteLog(DateTime.Now.ToString(), "TestJobTimer"); //Save invoke time to file

                ((Timer)timer).Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20));
            }
            catch(Exception ex)
            {
                string stop = ex.Message;
            }
        });
        JobTime.Change(0, 0);

Base on this sample , what i get is the timer will repeat every 5 second , thus what is the PERIOD paramenter for ?

Thank you

like image 205
abc cba Avatar asked May 07 '12 11:05

abc cba


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is CA procedural language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Why is C programming language important?

C is very fast in terms of execution time. Programs written and compiled in C execute much faster than compared to any other programming language. C programming language is very fast in terms of execution as it does not have any additional processing overheads such as garbage collection or preventing memory leaks etc.


2 Answers

dueTime shows when the first event will be fired,

period how often after that

in your case the first event will be fired after 5 second then after every 20 seconds

EDIT

As far as you are calling your timer change with 0,0, it starts impatiently and on timer tick call you change it to fire after 5 seconds every 20 second, that's why event fires every 5 seconds

If you want to fire event every 20 seconds after 5 seconds, remove timer change from handler, and start timer only once like this

    Timer JobTime = new Timer(timer =>
    {
        try
        {
            Console.WriteLine(DateTime.Now.ToString(), "TestJobTimer"); //Save invoke time to file
        }
        catch (Exception ex)
        {
            string stop = ex.Message;
        }
    });
    JobTime.Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20));

Hope this helps

like image 124
Arsen Mkrtchyan Avatar answered Oct 01 '22 16:10

Arsen Mkrtchyan


DueTime = time until first deployment

Period = amount of time after due time for next deployment, and amount of time for each subsequent deployment.

like image 32
spender Avatar answered Oct 01 '22 17:10

spender