CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Storyboard .... again

    Hi All,

    I use the following code;

    Code:
            private void StoryBoardCompleted(object sender, EventArgs e)
            {
                board.Stop(this);
                board.Remove(this);
                IsCompleted = true;
            }
    in

    Code:
                .
                .
                .
                Sound.Play();
                board = (Storyboard)TryFindResource("Storyboard1");
                board.Completed += new EventHandler(StoryBoardCompleted);
                board.Begin(this, true);
                while (IsCompleted != true)
                    ;
                this.Hide();
                .
                .
                .
    I want the above code to wait until the storyboard is finished
    I use the while statement to wait.
    But the program hangs on the while statement

    Any Help?

    Regards,

    Ger

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Storyboard .... again

    It doesn't hang, it spins and uses a lot of cpu so it appears to hang.

    In general, you never want to wait for a variable in an empty loop.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Storyboard .... again

    Btw, the file attachment download problem at code guru has been fix so you might want to zip up and post a sample solution that repros the issue.

  4. #4
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Re: Storyboard .... again

    Hi Arjay,

    Apart from Zip, what is the correct way to approach this.
    The Storyboard fires an iscompleted event, how do I wait for this to occur?

    regards,

    ger

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Storyboard .... again

    Simple - rather than having a method contain code that happens before and after the wait, you create an iscometed event handler and put the after the wait code in there.

    In general, in the main thread of a windows ui app, you should never write code that waits a significant amount of time (otherwise you will hang the ui).

    Instead you write an event handler which allows the main ui thread to continue running freely and you achieve the same effect as a blocking call, but the ui thread remains responsive.

    If you don't want the user to perform any actions, just disable some of the controls prior to creating the event handler and initiating the operation that will trigger the event.

    This same concept is used in any Windows program ( win32, mfc, winforms, wpf, etc) to keep the ui responsive.

  6. #6
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Re: Storyboard .... again

    Hi,

    Did it like this;
    Code:
             private void StoryBoardCompleted(object sender, EventArgs e)
            {
                board.Stop(this);
                board.Remove(this);
                this.Hide();
    
                COD01Window COD01 = new COD01Window();
                COD01.ShowDialog();
    
                
                Image_COD1.Visibility = Visibility.Visible;
                this.Show();
            }
    one small item, when the same button is pressed again in the main window, I have to close the COD01 window 2 times.
    and so on.
    in the COD01 window I use Close();

    regards,

    ger

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Storyboard .... again

    Solve the two problems separately. That is, get the popup modal for. Working before putting thst code in an event handler.

    Also, respond to my suggestions directly. Tell me if my suggestions have worked or have not and so on.

    I don't want to take the time to help, if you can't take the time to try the suggestions or respond to what I've written.

  8. #8
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Re: Storyboard .... again

    hi Arjay,

    did what you suggested;
    put the after wait code in the completed handler.
    Also tested the window issue non an separate test button
    separate they work fine now.
    But if I put the create and show window in the event handler, the dispose of the window is not done correctly.

    regards,

    Ger

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Storyboard .... again

    Check which thread the event handler executes in. Is it the same as the main ui thread?

  10. #10
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Re: Storyboard .... again

    Hi All,

    Solved the problem.
    Thanks to the hint of Arjay on threads.
    I'm using async and await
    like this;

    Code:
             private async void COD1_MouseUp(object sender, MouseButtonEventArgs e)
            {
                board = (Storyboard)TryFindResource("Storyboard1");
                Image_COD1.Visibility = Visibility.Hidden;
                board.Begin(this, true);
                await Task.Delay(2000);  // time it takes the animation to finish, 2 seconds
                board.Stop(this);
                this.Hide();
    
                COD01Window COD01 = new COD01Window();
                COD01.ShowDialog();
    
                Image_COD1.Visibility = Visibility.Visible;
                this.Show();
            }
    PS, you have to set the button(s) to IsEnabled to false;
    until the await Task.Delay has finished

    regards,

    Ger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured