执行长时间处理时,Windows Form中的GIF动画 [英] Animated GIF in Windows Form while executing long process
问题描述
我用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;
}
推荐答案
添加
您最终会得到这样的东西: 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 在这些事件中使用以下代码,请注意 The following code goes in these events, notice how Now you only need to start the background job, for example from a button click event to call 完成!您已经成功将背景工作人员添加到了表单中. Done! You have successfully added a backgroundworker to your form. 这篇关于执行长时间处理时,Windows Form中的GIF动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
DoWork
如何使用DowWorkEventArgs
Argument属性来检索RunWorkerAsync
中提供的值.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();
}
RunWorkerAsync
private void button1_Click(object sender, EventArgs e)
{
Loading_On();
backgroundWorker1.RunWorkerAsync(comboBox1.SelectedItem); // pass a string here
}