What is the best way to modify WPF's user interface asynchronously without freezing it?

Reza Jaferi 331 Reputation points
2022-12-31T00:32:16.53+00:00

Hi,
I used the Dispatcher property to modify the UI, and I added System.Threading.Thread.Sleep(3000) in line 38 to ensure that it would not freeze, but the UI stays frozen for 3 seconds at line 38.

According to the above, what is the best way to modify WPF's user interface without freezing it?

These are my C# codes:

        private async void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
        {  
            await Task.Run(() =>  
            {  
                OleDbCommand OleDbCommandInsert = null;  
                MessageWindow MW = null;  
                Dispatcher.Invoke(() =>  
                {  
                    OleDbParameter Parameter = new OleDbParameter();  
                    Parameter.OleDbType = OleDbType.Binary;  
                    Parameter.ParameterName = "BookImage";  
                    Parameter.Value = ImageToBytes((BitmapImage)BookImage.Source);  
                    OleDbCommandInsert = new OleDbCommand("Insert Into BookInfo(BookName,Authors,Publisher,[ISBN-13],[ISBN-10],Category,PublicationDate,BookLanguage,Length,[Type],Translators,Narrators,DeweyDecimalClassification,UniversalDecimalClassification,LibraryOfCongressClassification,BookImage,Price) Values(@BookName,@Authors,@Publisher,@ISBN13,@ISBN10,@Category,@PublicationDate,@BookLanguage,@Length,@Type,@Translators,@Narrators,@DeweyDecimalClassification,@UniversalDecimalClassification,@LibraryOfCongressClassification,@BookImage,@Price)", OleDbConnect);  
                    OleDbCommandInsert.Parameters.AddWithValue("@BookName", BookName_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Authors", Authors_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Publisher", PublicationDate_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@ISBN13", ISBN13_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@ISBN10", ISBN10_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Category", Category_ComboBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@PublicationDate", PublicationDate_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@BookLanguage", Language_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Length", ushort.Parse(Length_TextBox.Text));  
                    OleDbCommandInsert.Parameters.AddWithValue("@Type", Type_ComboBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Translators", Translators_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Narrators", Narrators_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@DeweyDecimalClassification", DeweyDecimalClassification_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@UniversalDecimalClassification", UniversalDecimalClassification_TextBox.Text);  
                    OleDbCommandInsert.Parameters.AddWithValue("@LibraryOfCongressClassification", LibraryOfCongressClassification_TextBox.Text);  
                    OleDbCommandInsert.Parameters.Add(Parameter);  
                    OleDbCommandInsert.Parameters.AddWithValue("@Price", Price_TextBox.Text);  
                });  
                OleDbConnect.Open();  
                OleDbCommandInsert.ExecuteScalar();  
                OleDbConnect.Close();  
                Dispatcher.Invoke(() =>  
                {  
                    MW = new MessageWindow();  
                    System.Threading.Thread.Sleep(3000);  
                    MW.YesButton.Visibility = Visibility.Hidden;  
                    MW.NoButton.Visibility = Visibility.Hidden;  
                    MW.Image.Source = GetImageFromBytes(System.IO.File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\Images\Check.bin"));  
                    switch (App.EnumLanguage)  
                    {  
                        case AllLanguage.English:  
                            MW.MessageTextBlock.Text = "Information added successfully";  
                            MW.OKButton.Content = "OK";  
                            break;  
                        default:  
                            MW.MessageTextBlock.Text = "اطلاعات با موفقیت اضافه شد";  
                            MW.OKButton.Content = "تایید";  
                            break;  
                    }  
                    MW.ShowDialog();  
                });  
            });  
        }  

I use the following tools:
• .NET Framework 4.5
• WPF
• OLE DB
• Windows 7

Thank you for your time.
Merry Christmas

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
{count} vote

Accepted answer
  1. رضا جافری 1,296 Reputation points
    2023-01-12T01:37:10.16+00:00

    I created an algorithm for this specific case with your assistance. For clearer understanding, I illustrated it as follows:

    enter image description here "XAML" (I only inserted the necessary codes):

        <Window x:Class="Ganj_Aseman.MessageWindow" x:Name="Message_Window"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:Ganj_Aseman"
                mc:Ignorable="d"
                Loaded="MessageWindow_Loaded" AllowsTransparency="True" Background="Transparent" FontSize="13" Height="228" RenderTransformOrigin="0.5,0.5" ResizeMode="NoResize" ShowInTaskbar="False" Width="430" WindowStartupLocation="CenterOwner" WindowStyle="None">
            <Window.RenderTransform>
                <TransformGroup>
                    <RotateTransform/>
                    <ScaleTransform/>
                </TransformGroup>
            </Window.RenderTransform>
            <Window.Resources>
                <Storyboard x:Key="CloseMessageWindow" Storyboard.TargetName="Message_Window" Completed="CloseMessageWindow_Completed">
                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].Angle" Duration="0:0:7" To="360"/>
                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:7" To="0"/>
                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:7" To="0"/>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:7.5" To="0"/>
                </Storyboard>
            </Window.Resources>
            <Grid>
                <Button Name="OKButton" GotFocus="OKButton_GotFocus" PreviewMouseLeftButtonDown="OKButton_PreviewMouseLeftButtonDown" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Margin="177.5,180,0,0">
                    <Button.Effect>
                        <DropShadowEffect BlurRadius="3" ShadowDepth="0.1" Direction="-90" Color="#e9f1cc"/>
                    </Button.Effect>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
                            <BeginStoryboard Storyboard="{StaticResource CloseMessageWindow}"/>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>
            </Grid>
        </Window>
    

    "C#" (owner window):

                private async void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                    await Task.Run(() =>
                    {
                        OleDbParameter Parameter = new OleDbParameter();
                        Parameter.OleDbType = OleDbType.Binary;
                        Parameter.ParameterName = "BookImage";
                        Dispatcher.Invoke(() =>
                        {
                            Parameter.Value = ImageToBytes((BitmapImage)BookImage.Source);
                        });
                        OleDbCommand OleDbCommandInsert = new OleDbCommand("Insert Into BookInfo(BookName,Authors,Publisher,[ISBN-13],[ISBN-10],Category,PublicationDate,BookLanguage,Length,[Type],Translators,Narrators,DeweyDecimalClassification,UniversalDecimalClassification,LibraryOfCongressClassification,BookImage,Price) Values(@BookName,@Authors,@Publisher,@ISBN13,@ISBN10,@Category,@PublicationDate,@BookLanguage,@Length,@Type,@Translators,@Narrators,@DeweyDecimalClassification,@UniversalDecimalClassification,@LibraryOfCongressClassification,@BookImage,@Price)", OleDbConnect);
                        Dispatcher.Invoke(() =>
                        {
                            OleDbCommandInsert.Parameters.AddWithValue("@BookName", BookName_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Authors", Authors_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Publisher", PublicationDate_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@ISBN13", ISBN13_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@ISBN10", ISBN10_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Category", Category_ComboBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@PublicationDate", PublicationDate_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@BookLanguage", Language_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Length", ushort.Parse(Length_TextBox.Text));
                            OleDbCommandInsert.Parameters.AddWithValue("@Type", Type_ComboBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Translators", Translators_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@Narrators", Narrators_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@DeweyDecimalClassification", DeweyDecimalClassification_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@UniversalDecimalClassification", UniversalDecimalClassification_TextBox.Text);
                            OleDbCommandInsert.Parameters.AddWithValue("@LibraryOfCongressClassification", LibraryOfCongressClassification_TextBox.Text);
                            OleDbCommandInsert.Parameters.Add(Parameter);
                            OleDbCommandInsert.Parameters.AddWithValue("@Price", Price_TextBox.Text);
                        });
                        OleDbConnect.Open();
                        OleDbCommandInsert.ExecuteScalar();
                        OleDbConnect.Close();
                        Dispatcher.Invoke(() =>
                        {
                            MessageWindow MW = new MessageWindow();
                            MW.Owner = this;
                            MW.Owner.IsEnabled = false;
                            MW.YesButton.Visibility = Visibility.Hidden;
                            MW.NoButton.Visibility = Visibility.Hidden;
                            MW.Image.Source = GetImageFromBytes(System.IO.File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\Images\Check.bin"));
                            switch (App.EnumLanguage)
                            {
                                case AllLanguage.English:
                                    MW.MessageTextBlock.Text = "Information added successfully";
                                    MW.OKButton.Content = "OK";
                                    break;
                                default:
                                    MW.MessageTextBlock.Text = "اطلاعات با موفقیت اضافه شد";
                                    MW.OKButton.Content = "تایید";
                                    break;
                            }
                            MW.Show();
                        });
                    });
                }
    

    "C#" (message window):

            private void OKButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                this.Owner.IsEnabled = true;
            }
            private void CloseMessageWindow_Completed(object sender, EventArgs e)
            {
                Close();
            }
    

    Result (I set the "Duration" property of the "Animation" to 7 seconds to better understand the result):

    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2022-12-31T02:09:03.197+00:00

    Dispatcher.Invoke executes the callback on the Thread that's associated with the Dispatcher (i.e. the one that's associated with the current control, which will be the UI thread). In this case you're running Thread.Sleep on the UI thread and that will freeze the UI.

    The idea is that you should only execute the code inside the callback that's relevant to updating controls (which are owned by the UI thread).

    Is there a reason that Thread.Sleep needs to be inside the callback? If you move it above the Dispatcher.Invoke call wouldn't it have the same behaviour?

    1 person found this answer helpful.

  2. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-12-31T11:48:23.637+00:00

    Hi,
    try this code:

    		private async void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
    		{  
    			OleDbCommand OleDbCommandInsert = new OleDbCommand("Insert Into BookInfo(BookName,Authors,Publisher,[ISBN-13],[ISBN-10],Category,PublicationDate,BookLanguage,Length,[Type],Translators,Narrators,DeweyDecimalClassification,UniversalDecimalClassification,LibraryOfCongressClassification,BookImage,Price) Values(@BookName,@Authors,@Publisher,@ISBN13,@ISBN10,@Category,@PublicationDate,@BookLanguage,@Length,@Type,@Translators,@Narrators,@DeweyDecimalClassification,@UniversalDecimalClassification,@LibraryOfCongressClassification,@BookImage,@Price)", OleDbConnect);  
    			OleDbParameter Parameter = new OleDbParameter();  
    			Parameter.OleDbType = OleDbType.Binary;  
    			Parameter.ParameterName = "BookImage";  
    			Parameter.Value = ImageToBytes((BitmapImage)BookImage.Source);  
    			OleDbCommandInsert.Parameters.AddWithValue("@BookName", BookName_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Authors", Authors_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Publisher", PublicationDate_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@ISBN13", ISBN13_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@ISBN10", ISBN10_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Category", Category_ComboBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@PublicationDate", PublicationDate_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@BookLanguage", Language_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Length", ushort.Parse(Length_TextBox.Text));  
    			OleDbCommandInsert.Parameters.AddWithValue("@Type", Type_ComboBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Translators", Translators_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Narrators", Narrators_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@DeweyDecimalClassification", DeweyDecimalClassification_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@UniversalDecimalClassification", UniversalDecimalClassification_TextBox.Text);  
    			OleDbCommandInsert.Parameters.AddWithValue("@LibraryOfCongressClassification", LibraryOfCongressClassification_TextBox.Text);  
    			OleDbCommandInsert.Parameters.Add(Parameter);  
    			OleDbCommandInsert.Parameters.AddWithValue("@Price", Price_TextBox.Text);  
    			await Task.Run(() =>  
    			{  
    				OleDbConnect.Open();  
    				OleDbCommandInsert.ExecuteScalar();  
    				OleDbConnect.Close();  
    				var img = GetImageFromBytes(System.IO.File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\Images\Check.bin"));  
    				System.Threading.Thread.Sleep(3000);  
    				Dispatcher.Invoke(() =>  
    				{  
    					MessageWindow MW = new MessageWindow();  
    					MW.YesButton.Visibility = Visibility.Hidden;  
    					MW.NoButton.Visibility = Visibility.Hidden;  
    					MW.Image.Source = img;  
    					switch (App.EnumLanguage)  
    					{  
    						case AllLanguage.English:  
    							MW.MessageTextBlock.Text = "Information added successfully";  
    							MW.OKButton.Content = "OK";  
    							break;  
    						default:  
    							MW.MessageTextBlock.Text = "اطلاعات با موفقیت اضافه شد";  
    							MW.OKButton.Content = "تایید";  
    							break;  
    					}  
    					MW.ShowDialog();  
    				});  
    			});  
    		}  
      
    

  3. رضا جافری 1,296 Reputation points
    2023-01-12T00:46:37.68+00:00
    I created an algorithm for this specific case with your assistance.
    For clearer understanding, I illustrated it as follows:
    
    
    XAML
    
            <Window x:Class="Ganj_Aseman.MessageWindow" x:Name="Message_Window"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:local="clr-namespace:Ganj_Aseman"
                    mc:Ignorable="d"
                    Loaded="MessageWindow_Loaded" AllowsTransparency="True" Background="Transparent" FontSize="13" Height="228" RenderTransformOrigin="0.5,0.5" ResizeMode="NoResize" ShowInTaskbar="False" Width="430" WindowStartupLocation="CenterOwner" WindowStyle="None">
                <Window.RenderTransform>
                    <TransformGroup>
                        <RotateTransform/>
                        <ScaleTransform/>
                    </TransformGroup>
                </Window.RenderTransform>
                <Window.Resources>
                    <Storyboard x:Key="CloseMessageWindow" Storyboard.TargetName="Message_Window" Completed="CloseMessageWindow_Completed">
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].Angle" Duration="0:0:7" To="360"/>
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:7" To="0"/>
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:7" To="0"/>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:7.5" To="0"/>
                    </Storyboard>
                </Window.Resources>
                <Grid>
                    <Button Name="OKButton" GotFocus="OKButton_GotFocus" PreviewMouseLeftButtonDown="OKButton_PreviewMouseLeftButtonDown" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Margin="177.5,180,0,0">
                        <Button.Effect>
                            <DropShadowEffect BlurRadius="3" ShadowDepth="0.1" Direction="-90" Color="#e9f1cc"/>
                        </Button.Effect>
                        <Button.Triggers>
                            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
                                <BeginStoryboard Storyboard="{StaticResource CloseMessageWindow}"/>
                            </EventTrigger>
                        </Button.Triggers>
                    </Button>
                </Grid>
            </Window>
    
    C#
    
                    private async void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                    {
                        await Task.Run(() =>
                        {
                            OleDbParameter Parameter = new OleDbParameter();
                            Parameter.OleDbType = OleDbType.Binary;
                            Parameter.ParameterName = "BookImage";
                            Dispatcher.Invoke(() =>
                            {
                                Parameter.Value = ImageToBytes((BitmapImage)BookImage.Source);
                            });
                            OleDbCommand OleDbCommandInsert = new OleDbCommand("Insert Into BookInfo(BookName,Authors,Publisher,[ISBN-13],[ISBN-10],Category,PublicationDate,BookLanguage,Length,[Type],Translators,Narrators,DeweyDecimalClassification,UniversalDecimalClassification,LibraryOfCongressClassification,BookImage,Price) Values(@BookName,@Authors,@Publisher,@ISBN13,@ISBN10,@Category,@PublicationDate,@BookLanguage,@Length,@Type,@Translators,@Narrators,@DeweyDecimalClassification,@UniversalDecimalClassification,@LibraryOfCongressClassification,@BookImage,@Price)", OleDbConnect);
                            Dispatcher.Invoke(() =>
                            {
                                OleDbCommandInsert.Parameters.AddWithValue("@BookName", BookName_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Authors", Authors_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Publisher", PublicationDate_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@ISBN13", ISBN13_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@ISBN10", ISBN10_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Category", Category_ComboBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@PublicationDate", PublicationDate_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@BookLanguage", Language_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Length", ushort.Parse(Length_TextBox.Text));
                                OleDbCommandInsert.Parameters.AddWithValue("@Type", Type_ComboBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Translators", Translators_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Narrators", Narrators_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@DeweyDecimalClassification", DeweyDecimalClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@UniversalDecimalClassification", UniversalDecimalClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@LibraryOfCongressClassification", LibraryOfCongressClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.Add(Parameter);
                                OleDbCommandInsert.Parameters.AddWithValue("@Price", Price_TextBox.Text);
                            });
                            OleDbConnect.Open();
                            OleDbCommandInsert.ExecuteScalar();
                            OleDbConnect.Close();
                            Dispatcher.Invoke(() =>
                            {
                                MessageWindow MW = new MessageWindow();
                                MW.Owner = this;
                                MW.Owner.IsEnabled = false;
                                MW.YesButton.Visibility = Visibility.Hidden;
                                MW.NoButton.Visibility = Visibility.Hidden;
                                MW.Image.Source = GetImageFromBytes(System.IO.File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\Images\Check.bin"));
                                switch (App.EnumLanguage)
                                {
                                    case AllLanguage.English:
                                        MW.MessageTextBlock.Text = "Information added successfully";
                                        MW.OKButton.Content = "OK";
                                        break;
                                    default:
                                        MW.MessageTextBlock.Text = "اطلاعات با موفقیت اضافه شد";
                                        MW.OKButton.Content = "تایید";
                                        break;
                                }
                                MW.Show();
                            });
                        });
                    }
    
    C#
    
                private void OKButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                    this.Owner.IsEnabled = true;
                }
                private void CloseMessageWindow_Completed(object sender, EventArgs e)
                {
                    Close();
                }
    Result (I set the 
    
    0 comments No comments

  4. رضا جافری 1,296 Reputation points
    2023-01-12T01:13:10.2+00:00
    I created an algorithm for this specific case with your assistance.
    For clearer understanding, I illustrated it as follows:
    
    
    XAML (I only inserted the necessary codes):
    
            <Window x:Class="Ganj_Aseman.MessageWindow" x:Name="Message_Window"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:local="clr-namespace:Ganj_Aseman"
                    mc:Ignorable="d"
                    Loaded="MessageWindow_Loaded" AllowsTransparency="True" Background="Transparent" FontSize="13" Height="228" RenderTransformOrigin="0.5,0.5" ResizeMode="NoResize" ShowInTaskbar="False" Width="430" WindowStartupLocation="CenterOwner" WindowStyle="None">
                <Window.RenderTransform>
                    <TransformGroup>
                        <RotateTransform/>
                        <ScaleTransform/>
                    </TransformGroup>
                </Window.RenderTransform>
                <Window.Resources>
                    <Storyboard x:Key="CloseMessageWindow" Storyboard.TargetName="Message_Window" Completed="CloseMessageWindow_Completed">
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].Angle" Duration="0:0:7" To="360"/>
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:7" To="0"/>
                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:7" To="0"/>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:7.5" To="0"/>
                    </Storyboard>
                </Window.Resources>
                <Grid>
                    <Button Name="OKButton" GotFocus="OKButton_GotFocus" PreviewMouseLeftButtonDown="OKButton_PreviewMouseLeftButtonDown" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Margin="177.5,180,0,0">
                        <Button.Effect>
                            <DropShadowEffect BlurRadius="3" ShadowDepth="0.1" Direction="-90" Color="#e9f1cc"/>
                        </Button.Effect>
                        <Button.Triggers>
                            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
                                <BeginStoryboard Storyboard="{StaticResource CloseMessageWindow}"/>
                            </EventTrigger>
                        </Button.Triggers>
                    </Button>
                </Grid>
            </Window>
    
    `C#` (owner window):
    
                    private async void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                    {
                        await Task.Run(() =>
                        {
                            OleDbParameter Parameter = new OleDbParameter();
                            Parameter.OleDbType = OleDbType.Binary;
                            Parameter.ParameterName = "BookImage";
                            Dispatcher.Invoke(() =>
                            {
                                Parameter.Value = ImageToBytes((BitmapImage)BookImage.Source);
                            });
                            OleDbCommand OleDbCommandInsert = new OleDbCommand("Insert Into BookInfo(BookName,Authors,Publisher,[ISBN-13],[ISBN-10],Category,PublicationDate,BookLanguage,Length,[Type],Translators,Narrators,DeweyDecimalClassification,UniversalDecimalClassification,LibraryOfCongressClassification,BookImage,Price) Values(@BookName,@Authors,@Publisher,@ISBN13,@ISBN10,@Category,@PublicationDate,@BookLanguage,@Length,@Type,@Translators,@Narrators,@DeweyDecimalClassification,@UniversalDecimalClassification,@LibraryOfCongressClassification,@BookImage,@Price)", OleDbConnect);
                            Dispatcher.Invoke(() =>
                            {
                                OleDbCommandInsert.Parameters.AddWithValue("@BookName", BookName_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Authors", Authors_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Publisher", PublicationDate_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@ISBN13", ISBN13_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@ISBN10", ISBN10_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Category", Category_ComboBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@PublicationDate", PublicationDate_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@BookLanguage", Language_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Length", ushort.Parse(Length_TextBox.Text));
                                OleDbCommandInsert.Parameters.AddWithValue("@Type", Type_ComboBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Translators", Translators_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@Narrators", Narrators_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@DeweyDecimalClassification", DeweyDecimalClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@UniversalDecimalClassification", UniversalDecimalClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.AddWithValue("@LibraryOfCongressClassification", LibraryOfCongressClassification_TextBox.Text);
                                OleDbCommandInsert.Parameters.Add(Parameter);
                                OleDbCommandInsert.Parameters.AddWithValue("@Price", Price_TextBox.Text);
                            });
                            OleDbConnect.Open();
                            OleDbCommandInsert.ExecuteScalar();
                            OleDbConnect.Close();
                            Dispatcher.Invoke(() =>
                            {
                                MessageWindow MW = new MessageWindow();
                                MW.Owner = this;
                                MW.Owner.IsEnabled = false;
                                MW.YesButton.Visibility = Visibility.Hidden;
                                MW.NoButton.Visibility = Visibility.Hidden;
                                MW.Image.Source = GetImageFromBytes(System.IO.File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\Images\Check.bin"));
                                switch (App.EnumLanguage)
                                {
                                    case AllLanguage.English:
                                        MW.MessageTextBlock.Text = "Information added successfully";
                                        MW.OKButton.Content = "OK";
                                        break;
                                    default:
                                        MW.MessageTextBlock.Text = "اطلاعات با موفقیت اضافه شد";
                                        MW.OKButton.Content = "تایید";
                                        break;
                                }
                                MW.Show();
                            });
                        });
                    }
    
    `C#` (message window):
    
                private void OKButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                    this.Owner.IsEnabled = true;
                }
                private void CloseMessageWindow_Completed(object sender, EventArgs e)
                {
                    Close();
                }
    Result (I set the "Duration" property of the "Animation" to 7 seconds to better understand the result):
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.