0

what i am trying to do is first find the angle of the shoulder joint till now i have managed to do that.what i wanted to do now is when the angle comes in the range between 70 and 90 a time starts for 3 sec and in each sec it should check whether the angle is still in the range if it is then show on screen ok otherwise restart the timer after displaying the message you have no completed the time limit Please please em very new to wold of C# and kinect any help in this regard will b helpful the link to an image of a problem em facing is: http://i46.tinypic.com/2nu4ygw.jpg

Please help!!

       System.Windows.Point shoul_l = this.point_toScreen(sh_left.Position, sen);
        draw.DrawText(new FormattedText(angle.ToString("0"), new 
        System.Globalization.CultureInfo("en-us"),
          System.Windows.FlowDirection.LeftToRight,
         new Typeface("Verdana"), 16,System.Windows.Media.Brushes.OrangeRed),
         new System.Windows.Point(shoul_l.X+10, shoul_l.Y +20));

        if (timer_start == false)
        {
        if (angle > 70 && angle < 90)
        {
                timer_start = true;
                timer.Interval = 2000;
                timer.Start();
                timer.Elapsed += new ElapsedEventHandler((sender, e) =>   \    
                on_time_event(sender, e, draw,shoul_l));

        }
        }


}   
void on_time_event(object sender, ElapsedEventArgs e, DrawingContext dcrt, 

 System.Windows.Point Shoudery_lefty)
 {
     --index;
     if (index != 0)
     {
         dcrt.DrawText(new FormattedText(index.ToString(), new   
      System.Globalization.CultureInfo("en-us"),
           System.Windows.FlowDirection.LeftToRight,
          new Typeface("Verdana"), 16, System.Windows.Media.Brushes.OrangeRed),
          new System.Windows.Point(Shoudery_lefty.X+50,Shoudery_lefty.Y+50));
      //  MessageBox.Show(index.ToString());
     }
     else
     {
         timer.Stop();
     }
   }

i get an exception in this part of the code

      dcrt.DrawText(new FormattedText(index.ToString(), new   
      System.Globalization.CultureInfo("en-us"),
      System.Windows.FlowDirection.LeftToRight,
      new Typeface("Verdana"), 16, System.Windows.Media.Brushes.OrangeRed),
      new System.Windows.Point(Shoudery_lefty.X+50,Shoudery_lefty.Y+50));
ahmad05
  • 438
  • 2
  • 6
  • 23

1 Answers1

2

You are trying to update a UI element from a Timer thread. This is not allowed.
You have to marshal the UI update call to the UI thread using the Dispatcher like this:

dcrt.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => 
    {
        dcrt.DrawText(new FormattedText(index.ToString(), new System.Globalization.CultureInfo("en-us"),
       System.Windows.FlowDirection.LeftToRight,
      new Typeface("Verdana"), 16, System.Windows.Media.Brushes.OrangeRed),
      new System.Windows.Point(Shoudery_lefty.X+50,Shoudery_lefty.Y+50));
    }));
khellang
  • 17,550
  • 6
  • 64
  • 84
  • i tried to replace your code with mine and it says Dispatcher and Dispatcher priority doesn't exist in current context decalring the new instance will not help the cause ... any help?? @khellang – ahmad05 Nov 01 '12 at 11:47
  • The `DrawingContext` derives from `DispatcherObject` which has `Dispatcher` as a property. Try `dcrt.Dispatcher.Invoke(...);` – khellang Nov 01 '12 at 11:50
  • You may also need to add a using statement: `using System.Windows.Threading;` – khellang Nov 01 '12 at 11:52
  • i know it might get irritating for u but there is still one more problem Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type "ignore" has this error – ahmad05 Nov 01 '12 at 11:56
  • Really appreciated your help Thank you so much but it didn't work for me same old problem exception at run time... this is the image http://i48.tinypic.com/vzzrqo.jpg – ahmad05 Nov 01 '12 at 12:12
  • can i return a integer value from a this event handler? and how can i get it at the calling end can i do like this int a = timer.Elapsed += new ElapsedEventHandler((sender, e) => \ on_time_event(sender, e, draw,shoul_l)); i tried to do this but it gave me error how can i get the value returned from event handler – ahmad05 Nov 01 '12 at 12:14