Issue
I have a button that performs an action that takes 3 seconds (reads data from an API and refreshes the screen). While the action is performed, I would like the button to rotate. My problem is that it starts to rotate AFTER the 3seconds operation has been completed... and, ofc it stops immediately, as it hits ClearAnimation..
Here's my code
this.refreshButton.Click += this.RefreshPendingOrders_Click;
private void RefreshPendingOrders_Click(object sender, EventArgs e)
{
this.StartRotateAnimation();
System.Threading.Thread.Sleep(3000);
this.refreshButton.ClearAnimation();
}
private void StartRotateAnimation()
{
var pivotX = this.refreshButton.Width / 2;
var pivotY = this.refreshButton.Height / 2;
var animation = new RotateAnimation(0f, 360f, pivotX, pivotY)
{
Duration = 500,
RepeatCount = 3
};
this.refreshButton.StartAnimation(animation);
}
Solution
The issue is that you are calling Thread.Sleep()
which in case of a Click EventHandler will be happening on the UI thread. This means you are blocking the UI Thread, which is responsible for running your animations for 3 seconds.
You could change your code to use async/await instead like:
private async void RefreshPendingOrders_Click(object sender, EventArgs e)
{
this.StartRotateAnimation();
await Task.Delay(3000);
this.refreshButton.ClearAnimation();
}
Answered By - Cheesebaron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.