Skip to content

How can i use any method in the timer ?

Faisal Ansari

Asked Apr 10, 2012, 1:38 AM EDT2 answers
Share
LinkedInX
hello guys how r u ?

my question is that, i have created a method which retrieve data from a service and i want to call that function in the timer  that when timer tick event fire  that function should be call ...  how can i do it ??


please reply soon


thanks

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi Faisal,

    You can do that. It supports a calling method ashyonchrously.. you can call the method in tick event,.

    ou're looking for Anonymous Methods:

    Code
    myTimer.Tick += delegate (object sender, EventArgs e) {
        MessageBox.Show("Hello world!");
    };
    You can also omit the parameters:

    Code
    myTimer.Tick += delegate {
        MessageBox.Show("Hello world!");
    };
    In C# 3.0, you can also use a Lambda Expression:

    Code
    myTimer.Tick += (sender, e) => {
        MessageBox.Show("Hello world!");
    };

    Normally the timer can  be written like this.

             myTimer.Tick += new EventHandler(TimerEventProcessor);

           // Sets the timer interval to 5 seconds.
           myTimer.Interval = 5000;
           myTimer.Start();

    It will call the method every time based on the interval set.

    You can get more info:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.tick.aspx


    0
  • 0
    sir its right  but  not right for me, i have an listview control which binded from List<class> in wpf application and i have to update that listview control on run time using Timer
    0