-
November 9th, 2013, 11:14 AM
#1
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
-
November 9th, 2013, 12:17 PM
#2
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.
-
November 9th, 2013, 02:01 PM
#3
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
-
November 9th, 2013, 12:18 PM
#4
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.
-
November 9th, 2013, 02:38 PM
#5
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.
-
November 9th, 2013, 02:52 PM
#6
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
-
November 9th, 2013, 03:19 PM
#7
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.
-
November 9th, 2013, 03:33 PM
#8
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
-
November 9th, 2013, 06:13 PM
#9
Re: Storyboard .... again
Check which thread the event handler executes in. Is it the same as the main ui thread?
-
November 14th, 2013, 04:24 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|