在执行数据库操作时,无法在WPF中显示GIF加载程序

时间:2016-02-01 12:12:34

标签: c# wpf backgroundworker ui-thread

您好我正在尝试在一些耗时操作执行时显示GIf图像。

我尝试使用以下代码..我能够看到加载窗口但gif图像的动画不起作用..当搜索解决方案时,我发现方法更新UI,而它被背景功能冻结。但它仍然无法正常工作。这是我的Loader Window XAML代码:

<Window x:Class="project1.Views.loader" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  xmlns:gif="http://wpfanimatedgif.codeplex.com"
  gif:ImageBehavior.AnimateInDesignMode="True"
  Title="Loading" Height="180" Width="461" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="SingleBorderWindow" Loaded="Window_Loaded">
 <StackPanel Name="LoadingData" Background="White" >
    <Label x:Name="lblprogressmessage" x:FieldModifier="public" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,35,0,10" Content="" Foreground="#FF1E8ED4" FontSize="14"></Label>
    <Image x:Name="imggif" gif:ImageBehavior.RepeatBehavior="Forever"  gif:ImageBehavior.AnimatedSource="pack://application:,,,/project1;component/Images/ajaxloader.gif" Height="50px" Width="60px"/>
 </StackPanel>
</Window>

尝试在另一个用户控制窗口中显示此窗口,该窗口具有按钮,当点击此处时将执行一些数据库操作是我的按钮代码

loaderWindow ld = new loaderWindow // object to show loader window
Sample ViewModel = new Sample();

private void btn_Click(object sender, RoutedEventArgs e)
{
   try
    {          
      SampleEntityManager objem = new SampleEntityManager ();
      using (var dbContext = Database1.getDBContext())
      {
        if(objem .checkFundExist(dbContext,Guid.Parse(cbx1SelectedValue.ToString()), Guid.Parse(cbx2.SelectedValue.ToString()), int.Parse(cbx3.SelectedValue.ToString())) == false)
          {
            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerSupportsCancellation = true;
            bw.WorkerReportsProgress = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            ForceUIToUpdate();
            bw.RunWorkerAsync();
            ld.lblprogressmessage.Content = "Creating Fund for Year "+cbx1.Text +" Please Wait. . .";

           *ld.Show();* // calling show method to display loader window
          }
         else
          {
            MessageBox.Show("Fund Has Already Been Created", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
           }
         }
      }       
      catch (Exception es)
       {
         MessageBox.Show("Invalid Opertaion try again.If Problem Persists Contact Support", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
       }
 }

这里是在冻结时强制更新UI的方法

    // method to update UI
    public static void ForceUIToUpdate()
    {
        DispatcherFrame frame = new DispatcherFrame();

        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate (object parameter)
        {
            frame.Continue = false;
            return null;
        }), null);

        Dispatcher.PushFrame(frame);
    }

后台工作者代码如下

  private void bw_DoWork(object sender, DoWorkEventArgs e)
  {
        BackgroundWorker worker = sender as BackgroundWorker;
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
        }
        else
        {
            // Perform a time consuming operation and report progress.

            this.Dispatcher.InvokeAsync((Action)(() =>
            {
               // Time Consuming Database method which will set Fund entry Field for all the employess in Union
                ViewModel .CreateFund(cbx1.SelectedItem, cbx2.SelectedItem,cbx3.SelectedValue.ToString(), txt_desc.Text);
                cbx1.SelectedIndex = -1;
                cbx2.SelectedIndex = -1;
                **ld.Hide();** // calling Hide Method close Loader window once database opertaion completed
            }), System.Windows.Threading.DispatcherPriority.Background);
        }
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;
        if ((e.Cancelled == true))
        {
            ld.lblprogressmessage.Content = "Cancelled !";
        }
        else if (!(e.Error == null))
        {
            ld.lblprogressmessage.Content = ("Error  : " + e.Error.Message);
        }
        else
        {
            if (worker.WorkerSupportsCancellation == true)
            {
                worker.CancelAsync();
            }
        }
    }

请建议我完成任务的方法..谢谢

1 个答案:

答案 0 :(得分:1)

您使用的是BackgroundWorker错误。您正在从Dispatcher.InvokeAsync方法调用DoWork,该方法实际上是在UI线程上执行代码,而不是后台线程,因此阻止了UI。您需要做的是找到一种在后台实际执行耗时代码的方法,然后在工作完成后仅从RunWorkerCompleted更新UI,或者如果需要,请使用ReportProgress在执行期间更新UI。

How to: Use a Background Worker

此外,在启动worker时显示加载器窗口,就像你已经做过的那样,但是调用方法将其隐藏在RunWorkerCompleted之后,DoWork在backgroud线程完成后在UI线程上执行,而不是PlacesService方法。