Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何显示加载屏幕直到我的功能完成_C#_Wpf_Excel_Loading_Splash Screen - Fatal编程技术网

C# 如何显示加载屏幕直到我的功能完成

C# 如何显示加载屏幕直到我的功能完成,c#,wpf,excel,loading,splash-screen,C#,Wpf,Excel,Loading,Splash Screen,我有一个名为Export Excel的函数,它基本上将数据导出到Excel并保存。 因此,我需要显示一个启动屏幕,直到我的功能完成工作。我怎样才能做到这一点 已编辑:如何在显示以下对话框之前关闭“请稍候”屏幕 我的代码片段: //对于后台工作人员: public Form1() { InitializeComponent(); backgroundWorker1.WorkerReportsProgress = true; backgrou

我有一个名为Export 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);
    }
基本Windows应用程序运行在一个线程上,通常称为UI线程

你可以找个幕后工作者。它在一个独立于GUI的线程上工作

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


很少有文章可供参考:&

我建议结合使用BackgroundWorker和ProgressBar。由于您不知道处理是否会在将来增加,因此最好在单独的线程上进行,这样,如果用户在单击按钮后尝试与父窗体交互,它就不会冻结

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

a

b

您可以在同一表单上添加进度条,也可以使用类似弹出窗口的小用户控件来显示进度条和文本。可以从后台工作人员BackgroundWorker.ProgressChanged事件发送进度更新。因此,您将ExportInXcel分块,并将更新发送到主窗体,主窗体将更新进度条10%…20%…30%。完成最后一次处理后,您可以对用户隐藏进度条

编辑:添加了示例


这是winform应用程序吗?你看过System.Windows.Forms.ProgressBar类了吗?@PiyushParashar我在Windows应用程序中使用这个函数,你认为这个函数会花费很多时间吗?我问这个问题的原因取决于时间的顺序,例如@piyusparashar我不知道将来是否有机会增加时间。。我真的说不上合适的时间。。不同的投入不同time@PiyushParashar我知道了..请你帮我关闭我的spalsh屏幕,当它显示一个dupilcaite文件时。。或者在我的信息框前。。请看一下我编辑过的问题。谢谢你的例子。。我找到了很多例子。在谷歌,但我不知道如何与我的功能互动。。这是我悲哀的一点,首先你需要理解给定的例子。从按钮开始单击并逐步调试功能。理解了这个概念后,考虑一下与UI的交互。@请查看我的更新代码。。我已经测试了这个后台工作人员。。在那里我可以从主窗体运行第二次。。一分钟后把它关上。。但我现在不知道如何与我的功能交互。。你能更新你的答案吗谢谢你的例子。。我找到了很多例子。在谷歌,但我不知道如何与我的功能互动。。我的悲伤问题请查看我的更新代码。。因为我正在加载启动屏幕几秒钟,然后加载表单2。。使用背葫芦工人。。这就是我从这些例子中所能做到的。。我不知道如何使用此后台工作程序调用我的函数导出Excel。。你能更新一下你的答案吗。。带有清晰的代码。。非常感谢你的欢迎。我可以问一下,为什么这没有被标记为答案?你还需要什么吗?
//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.
}