I'm using C# WPF and Blend for Visual Studio 2019 I have a button when user click on it it will get data from the SQL Server database
Also, a made Animation for this button in Blend that will run rotate animation OnPreviewMouseDown
But the problem is when I click the button in one-moment animation is running but suddenly stopped
I think this problem is because of the button on Click event running some code and that prevent to animation continue
Long story short: the animation for the button not working when event click is processin
in Blend :
XAML :
<Window.Resources>
<Storyboard x:Key="RotationBtn">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="Command4">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="720"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.PreviewMouseDown" SourceName="Command4">
<BeginStoryboard x:Name="RotationBtn_BeginStoryboard" Storyboard="{StaticResource RotationBtn}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Button x:Name="Command4" Content="Next Stage" Margin="599,406,304,84" Width="130" Height="37" FontSize="8" Click="Command4_Click" Background="#FFDDDDDD" BorderBrush="{x:Null}" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
</Button>
</Grid>
CS Code :
private void Command4_Click(object sender, RoutedEventArgs e)
{
new Thread(() =>
{
App.Current.Dispatcher.Invoke((Action)delegate
{
Storyboard sb = TryFindResource("RotationBtn") as Storyboard;
Storyboard.SetTarget(sb, Command4);
sb.Begin();
});
}).Start();
var MyQuery = dbms.Database.SqlQuery<DB_Cust>("SELECT * FROM Customers").ToList();
}
Result of The result of the upper lines ↑ :
as you can see it stop
what I need is something like this simulated :
keep animation show when that's runnig this :
var MyQuery = dbms.Database.SqlQuery<DB_Cust>("SELECT * FROM Customers").ToList();
My Thread for animation does not work correctly it mean's when I click on the button
in a few moment animation will run but after In a short moment suddenly animation will stop until the code of query for get data from database in Click Event To end
how to fix this I want to show animation until code is processing in an event
Please help and thanks for taking your time for answer this question
Edit : When I use this code :
if(string.IsNullOrEmpty(MyTextBox))
{
//Do somthing
}
I Have This Error :
The calling thread cannot access this object because a different thread owns it
and Next I have tried This:
Dispatcher.Invoke(new Action(() =>
{
if (string.IsNullOrEmpty(MAH.Text))
{
//Do somthing
}
}));
but again my program will freeze or lock until process this :string.IsNullOrEmpty(MAH.Text) and animation will stop
Please Help?
My Project Link :
kj8BXD2Q
in One Drive :
s!Aq2vCYipSlKYggAypH3MeVMIMV5I