我有一个名为导出Excel 的功能,它基本上将数据导出到Excel并保存。 所以我需要显示一个启动画面,直到我的功能完成工作。我怎么能这样做。
已编辑:如何在显示以下对话框之前关闭我的请稍候屏幕
我的代码段:
//对于背景工作者:
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);
}
答案 0 :(得分:0)
基本 Windows应用程序在单线程上运行,通常称为UI线程。
您可以使用BackgroundWorker
。它正在与GUI的单独线程上工作。
此对象旨在简单地在不同的thread
上运行一个函数,然后在UI线程上调用一个事件。
可用的文章很少:Example Project &amp; Multithreading
答案 1 :(得分:0)
我建议使用BackgroundWorker和ProgressBar的组合。由于您不知道将来是否可以增加处理,因此最好在单独的线程上执行此操作,以便在用户单击按钮后尝试与父窗体交互时它不会冻结。
您可以查看以下有关如何使用ProgressBar控件和BackgroundWorker的文章:
您可以在同一表单上添加进度条,也可以使用像用户控件这样的小弹出窗口来显示进度条和文本。可以从后台工作程序(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() i.e. 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.
}