0

Good day to all of you. Can anyone tell me how can I start performing my function repeatedly every second, when my app starts? I have set up timer according to some examples I found here and through google. However, when I run my app nothing is happeneing.

here is the code

  public void kasifikuj()
    {
        if (File.Exists(@"E:\KINECT\test.txt"))
        {
            File.Delete(@"E:\KINECT\test.txt");
        }
        File.AppendAllText(@"E:\KINECT\test.txt", shoulderRightY + " " + shoulderLeftY + " " + headY + " " + hipY + Environment.NewLine);

        double detect = Program.siet();
        vysledok.Text = detect.ToString();


    }

    private Timer timer1;
    public void InitTimer()
    {
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000; // in miliseconds
        timer1.Start();
    }

    public void timer1_Tick(object sender, EventArgs e)
    {
        kasifikuj();
    }

EDIT:

Or can you suggest another way to run my kasifikuj() method every second please?

user2886091
  • 725
  • 7
  • 16
  • 29

4 Answers4

0

Perhaps it has something to do with:

you don't call your function InitTimer or you haven't enabled your timer. I work with c# WindowsFormsApplications. I don't know if this is different from WPF.

I hope it helps

RvA
  • 43
  • 1
  • 1
  • 3
0

Mainly RvA is correct, you are not calling InitTimer, or atleast not in the code you have provided in your question.

When you call InitTimer, your code should work pretty well. However I'd advice you to have a look at TPL (Task Parallel Libary) which is explained in this stackoverflow question: Proper way to implement a never ending task. (Timers vs Task)

Community
  • 1
  • 1
Frederik Prijck
  • 1,424
  • 10
  • 16
0

I found something similar on here a while back, i think.... Here

 public static class DelayedExecutionService
{
    public static void DelayedExecute(Action action, int delay = 1)
    {
        var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

        EventHandler handler = null;
        handler = (sender, e) =>
        {
            // Stop the timer so it won't keep executing every X seconds
            // and also avoid keeping the handler in memory.
            //dispatcherTimer.Tick -= handler;
            //dispatcherTimer.Stop();

            // Perform the action.
            action();
        };

        dispatcherTimer.Tick += handler;
        dispatcherTimer.Interval = TimeSpan.FromSeconds(delay);
        dispatcherTimer.Start();
    }
}

Use it like this

DelayedExecutionService.DelayedExecute(RefreshLList, 1);
DelayedExecutionService.DelayedExecute(UpdateLList, 1);

Where RefreshLList and UpdateLList are defined as void func();

Hope this helps.

TheNoob
  • 861
  • 2
  • 11
  • 25
0

You should subscribe to the Window's Loaded event and call InitTimer() function in event handler:

    public SomeWindow()
    {
        Loaded += SomeWindow_Loaded;
    }

    private void SomeWindow_Loaded(object sender, RoutedEventArgs e)
    {
        InitTimer();
    }
Indian
  • 529
  • 1
  • 12
  • 25