执行长时间处理时,Windows Form中的GIF动画 [英] Animated GIF in Windows Form while executing long process

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

问题描述

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

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

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

I am using ClosedXML to achieve this successfully.

执行该过程后,我要显示一个包含动画GIF图像的图片框.

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.

我看到很多帖子说使用从未使用过的backgroundworker或线程,发现很难实现.

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

推荐答案

添加

  • 在设计器中打开表单
  • 打开工具箱( ctrl + alt + X )
  • 打开类别组件"
  • 在您的发件人"中拖动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:

    您现在可以在属性"选项卡上切换到事件视图,并为 RunWorkerCompleted

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

    在这些事件中使用以下代码,请注意DoWork如何使用DowWorkEventArgs Argument属性来检索RunWorkerAsync中提供的值.

    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 Form中的GIF动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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