执行长过程时 Windows 窗体中的动画 GIF [英] Animated GIF in Windows Form while executing long process

查看:16
本文介绍了执行长过程时 Windows 窗体中的动画 GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 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;}私有无效 Loading_Off(){Cursor.Current = Cursors.Arrow;pictureBox2.Visible = false;groupBox1.Enabled = true;groupBox5.Enabled = true;groupBox6.Enabled = true;Cursor.Current = Cursors.WaitCursor;}

解决方案

添加 和

 private void button1_Click(object sender, EventArgs e){Loading_On();backgroundWorker1.RunWorkerAsync(comboBox1.SelectedItem);//这里传递一个字符串}

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

I have developed a simple windows Application(MDI) in C# which exports the data from SQL to Excel.

I am using ClosedXML to achieve this successfully.

When the process is executed, I want to show a picturebox containing a animated GIF image.

I am a beginner and don't know how to achieve this, the picturebox appears after the process is completed.

I saw lot of posts which says to use backgroundworker or threading which I have never used and finding it hard to implement.

Can I have an step by step example with explanation.

The two functions which I have created which I call before and after I execute the code.

        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;
    }

解决方案

It is not that hard to add a BackgroundWorker

  • Open your form in the designer
  • Open the Toolbox (ctrl+alt+X)
  • Open the category Components
  • Drag the Backgroundworker on your From

You will end-up with something like this:

You can now switch to the event view on the Properties tab and add the events for DoWork and RunWorkerCompleted

The following code goes in these events, notice how DoWork use the DowWorkEventArgs Argument property to retrieve the value that is supplied in 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();
    }

Now you only need to start the background job, for example from a button click event to call RunWorkerAsync

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

Done! You have successfully added a backgroundworker to your form.

这篇关于执行长过程时 Windows 窗体中的动画 GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆