美文网首页
WPF 多线程解决假死问题

WPF 多线程解决假死问题

作者: Aaron_Swartz | 来源:发表于2020-09-19 17:52 被阅读0次
    • xmal 代码
    <Window x:Class="ThreadTest.MainWindow"
            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:ThreadTest"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="41,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <DataGrid AutoGenerateColumns="False" Height="99" HorizontalAlignment="Left" Margin="40,70,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="441" />
            <Canvas Name="loading" RenderTransformOrigin="0.5,0.5" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center" Width="120" Height="120" Margin="187,76,241,85">
                <Ellipse Width="15" Height="15" Canvas.Left="55" Canvas.Top="31" Stretch="Fill" Fill="#FFD1D1F7" Opacity="1.0"/>
                <Ellipse Width="15" Height="15" Canvas.Left="38" Canvas.Top="39" Stretch="Fill" Fill="Blue" Opacity="0.8"/>
                <Ellipse Width="15" Height="15" Canvas.Left="36" Canvas.Top="58" Stretch="Fill" Fill="#FF0000FE" Opacity="0.7"/>
                <Ellipse Width="15" Height="15" Canvas.Left="52" Canvas.Top="67" Stretch="Fill" Fill="Blue" Opacity="0.6"/>
                <Ellipse Width="15" Height="15" Canvas.Left="68" Canvas.Top="61" Stretch="Fill" Fill="#FF2E2EFF" Opacity="0.5"/>
                <Ellipse Width="15" Height="15" Canvas.Left="69" Canvas.Top="42" Stretch="Fill" Fill="#FF6F6FFF" Opacity="0.4"/>
                <Canvas.RenderTransform>
                    <RotateTransform x:Name="SpinnerRotate" Angle="0" />
                </Canvas.RenderTransform>
                <Canvas.Triggers>
                    <EventTrigger RoutedEvent="ContentControl.Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.8" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Canvas.Triggers>
            </Canvas>
            <Label Name="lab_pro" Content="Label" Height="28" Margin="140,14,315,239" />
        </Grid>
    </Window>
    
    
    • 后台代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    
    namespace ThreadTest
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            BackgroundWorker bgMeet;
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                bgMeet = new BackgroundWorker();
                bgMeet.WorkerReportsProgress = true;
                bgMeet.DoWork += new DoWorkEventHandler(bgMeet_DoWork);
                bgMeet.ProgressChanged += new ProgressChangedEventHandler(bgMeet_ProgressChanged);
                bgMeet.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgMeet_RunWorkerCompleted);
                bgMeet.RunWorkerAsync();
            }
            void bgMeet_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                loading.Visibility = System.Windows.Visibility.Collapsed;
                this.Dispatcher.Invoke(new Action(() =>
                {
                    this.lab_pro.Content = "完成";
                }));
            }
            void bgMeet_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    this.lab_pro.Content = e.ProgressPercentage;
                }));
            }
    
            void bgMeet_DoWork(object sender, DoWorkEventArgs e)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    loading.Visibility = System.Windows.Visibility.Visible;
                }));
                GetData();
            }
            public void GetData()
            {
                for (int i = 0; i < 11; i++)
                {
                    bgMeet.ReportProgress(i);
                    System.Threading.Thread.Sleep(500);
                }
            }
        }
    }
    
    

    参考;
    1 WPF防止界面卡死并显示加载中效果
    2 BackgroundWorker原理剖析

    相关文章

      网友评论

          本文标题:WPF 多线程解决假死问题

          本文链接:https://www.haomeiwen.com/subject/wtfyyktx.html