【问题标题】:Animated GIF in Windows Form while executing long processWindows 窗体中的动画 GIF,同时执行长进程
【发布时间】:2015-07-10 10:17:19
【问题描述】:

我用 C# 开发了一个简单的 Windows 应用程序 (MDI),它可以将数据从 SQL 导出到 Excel。

我正在使用 ClosedXML 成功实现这一目标。

当执行该过程时,我想显示一个包含动画 GIF 图像的图片框。

我是初学者,不知道如何实现,完成后出现图片框。

我看到很多帖子说要使用我从未使用过的后台工作程序或线程,但发现它很难实现。

我可以有一个带有解释的分步示例吗?

我创建的两个函数,我在执行代码之前和之后调用。

        private void Loading_On()
    {
        Cursor.Current = Cursors.WaitCursor;
        pictureBox2.Visible = true;
        groupBox1.Enabled = false;
        groupBox5.Enabled = false;
        groupBox6.Enabled = false;
        Cursor.Current = Cursors.Arrow;
    }


    private void Loading_Off()
    {
        Cursor.Current = Cursors.Arrow;
        pictureBox2.Visible = false;
        groupBox1.Enabled = true;
        groupBox5.Enabled = true;
        groupBox6.Enabled = true;
        Cursor.Current = Cursors.WaitCursor;
    }

【问题讨论】:

  • 我已使用 Thread 成功显示图片框,但组框未禁用。

标签: c# picturebox animated-gif


【解决方案1】:

添加BackgroundWorker并不难

  • 在设计器中打开您的表单
  • 打开工具箱 (ctrl+alt+X)
  • 打开组件类别
  • 将 Backgroundworker 拖到您的 From 上

你最终会得到这样的结果:

您现在可以切换到“属性”选项卡上的事件视图并为DoWorkRunWorkerCompleted 添加事件

以下代码进入这些事件,注意DoWork 如何使用DowWorkEventArgs 参数属性来检索RunWorkerAsync 中提供的值。

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // start doing what ever needs to be done
        // get the argument from the EventArgs
        string comboboxValue = (string) e.Argument; // if Argument isn't string, this breaks
        // remember that this is NOT on the UI thread

        // do a lot of work here that takes forever
        System.Threading.Thread.Sleep(10000);
        // afer this the completed event is fired
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // this runs on the UI thread
        Loading_Off();
    }

现在你只需要启动后台作业,例如从按钮点击事件调用RunWorkerAsync

    private void button1_Click(object sender, EventArgs e)
    {
         Loading_On();
         backgroundWorker1.RunWorkerAsync(comboBox1.SelectedItem); // pass a string here
    }

完成!您已成功将后台工作人员添加到表单中。

【讨论】:

    【解决方案2】:

    实现这一点的最佳方法是在异步任务中运行动画,但相应的一些限制是可以使用线程睡眠在 Windows 窗体上执行此操作。

    例如:在你的构造函数中,

    public partial class MainMenu : Form
    {
    
        private SplashScreen splash = new SplashScreen();
    
        public MainMenu ()
        {
            InitializeComponent();
    
            Task.Factory.StartNew(() => {
                splash.ShowDialog();
            });
    
           Thread.Sleep(2000);
       }
    

    在启动一个新线程后将线程休眠非常重要,不要忘记您在该线程上执行的每个操作都需要调用,例如

        void CloseSplash(EventArgs e)
        {
            Invoke(new MethodInvoker(() =>
            {
               splash.Close();
            }));
        }
    

    现在你的 gif 应该可以工作了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-11
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2010-09-15
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多