如何显示加载屏幕,直到我的function完成

我有一个名为Export Excel的函数,它基本上将数据导出到excel并保存。 所以我需要显示一个启animation面,直到我的function完成工作。 我怎样才能做到这一点。

编辑: 如何在显示下面的对话框之前closures我的Please wait屏幕

我的代码片段:

//对于后勤人员:

public Form1() { InitializeComponent(); backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true; _f2 = new Form2(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //backgroundWorker1.DoWork += new DoWorkEventHandler(this.ExportInExcel); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { _f2.Hide(); } private void button1_Click(object sender, EventArgs e) { _f2.Show(); backgroundWorker1.RunWorkerAsync(); ExportInExcel(lstExcel, @"Z:\Desktop\myExcel.xls"); // i need to show the splash screen and //at the same time i need to do the function .. and close the splash screen after it //completes the job. } private void ExportInExcel(SortedDictionary<string, ExcelData> lstData, string excelPath) { //some codes for precessing the file to excel xlApp.Quit(); //xlApp.Close(false); //xlApp.Quit(); Process[] pro = Process.GetProcessesByName("excel"); pro[0].Kill(); pro[0].WaitForExit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } 

我会build议使用BackgroundWorker和ProgressBar的组合。 既然你不知道未来处理是否会增加,最好是在单独的线程上进行处理,这样如果用户在点击button后试图与父表单交互,它就不会冻结。

您可以查看以下有关如何使用ProgressBar控件和BackgroundWorker的文章:

a) ProgressBar

b) BackgroundWorker

您可以将进度条添加到相同的表单上,或者使用一个小的popup式用户控件来显示进度条和文本。 进度更新可以从后台工作人员(BackgroundWorker.ProgressChanged事件)发送。 因此,您将ExportInExcel分成大块,并将更新发送到将更新进度条(10%…. 20%…. 30%)的主窗体。 一旦处理的最后处理结束,您可以隐藏用户的进度条。

编辑:添加示例

 //Create a background worker private System.ComponentModel.BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); //Add event handler to the background worker in your main form_load event // tell the background worker it can report progress backgroundWorker1.WorkerReportsProgress = true; // add our event handlers backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.RunWorkerCompleted); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(this.ProgressChanged); //This is the method to do background work. Can be in a separate class. backgroundWorker1.DoWork += new DoWorkEventHandler(this.ExportInExcel); //On button click tell the worker to start. It will call the event handler of DoWork() ie ExportInExcel backgroundWorker1.RunWorkerAsync(); //In ExportInExcel update the progress when some part of work finishes. Pay attention to the signature of the method. It should of type DoWorkEventHandler public void ExportInExcel(object sender, EventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //process first step worker.ReportProgress(10, "Setting up the query"); //and so on } //Progress changed from the worker public void ProgressChanged(object sender, ProgressChangedEventArgs e) { //update progress bar } private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //Work complete. Hide progress bar etc. } 

一个基本的Windows应用程序运行在通常称为UI线程的单个线程上。

你可以使用BackgroundWorker 。 它正在从GUI的另一个线程上工作。

这个对象被devise成简单地在不同的thread上运行一个函数,然后在UI线程完成时调用一个事件

有几篇可用的文章: 例子项目 multithreading