multithreading - How do I block access to a method until animations are complete -


i have silverlight app. has basic animation rectangle animated new position. animation consists of 2 doubleanimation() - 1 transforms x, other transforms y. works ok.

i want block other calls animate method until first 2 animations have completed. see doubleanimation() class has completed event fires haven't been successful in constructing kind of code blocks until both have completed.

i attempted use monitor.enter on private member when entering method, releasing lock 1 of animations completed event, attempts @ chaining 2 events (so lock isn't released until both have completed) haven't been successful.

here's animation method looks like:

    public void animaterectangle(rectangle rect, double newx, double newy)     {             var xiscomplete = false;              duration duration = new duration(new timespan(0, 0, 0, 1, 350));             var easing = new elasticease() { easingmode = easingmode.easeout, oscillations = 1, springiness = 4 };             var animatex = new doubleanimation();             var animatey = new doubleanimation();              animatex.easingfunction = easing;             animatex.duration = duration;             animatey.easingfunction = easing;             animatey.duration = duration;              var sb = new storyboard();              sb.duration = duration;             sb.children.add(animatex);             sb.children.add(animatey);              storyboard.settarget(animatex, rect);             storyboard.settargetproperty(animatex, new propertypath("(canvas.left)"));             storyboard.settarget(animatey, rect);             storyboard.settargetproperty(animatey, new propertypath("(canvas.top)"));              animatex.to = newx;             animatey.to = newy;             sb.begin();      } 

edit (added more info)

i ran because calling method method (as processed items made call animation). noticed items didn't end expected them to. new x/y coordinates pass in based on items current location, if called multiple times before finished, ended in wrong location. test added button ran animation once. worked. however, if click on button bunch of times in row see same behavior before: items end in wrong location.

yes, appears silverlight animations run on main ui thread. 1 of tests tried added 2 properties flagged whether both animations had completed yet. in animaterectange() method checked them inside of while loop (calling thread.sleep). loop never completed (so it's on same thread).

so created queue process animations in order:

    private void processanimationqueue()     {         var items = this.m_animationqueue.getenumerator();         while (items.movenext())         {             while (this.m_isxanimationinprogress || this.m_isyanimationinprogress)             {                 system.threading.thread.sleep(100);             }              var item = items.current;             dispatcher.begininvoke(() => this.animaterectangle(item.rect.rect, item.x, item.y));                         }     } 

then call initial routine (which queues animations) , call method on new thread. see same results.

as far aware of animations in silverlight happening on ui thread anyway. guessing ui thread calling animation function anyway, not sure using locking help. want blocking entire thread or preventing animation starting?

i suggest more this:

private bool isanimating = false;  public void animaterectangle(rectangle rect, double newx, double newy) {     if (isanimating)         return;      // rest of animation code      sb.completed += (sender, e) =>     {         isanimating = false;     };      isanimating = true;     sb.begin(); } 

just keep track of whether or not animating flag , return if are. if don't want lose potential animations other option keep kind of queue animation check/start when each animation has completed.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -