1

I have a function called Export Excel which basically exports data to excel and saves it. So i need to show a splash screen till my function completes the job. How can i do this.

EDITED : How to close my Please wait screen before showing the below dialog box

My code snippet:

//For back ground worker:

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);
    }
  • 1
    Is this a winform application? Did you take a look at System.Windows.Forms.ProgressBar class? – Piyush Parashar Nov 20 '14 at 03:59
  • @PiyushParashar i use this for windows application –  Nov 20 '14 at 04:01
  • Are you expecting this function to take a lot of time? Why I ask is depending on the order of time (e.g <2 sec or 5-10 sec) taken for this a good solution can be suggested. Do think about what can happen in future to this? Is there a possibility that the input data will increase substantially which can affect your overall time? – Piyush Parashar Nov 20 '14 at 04:10
  • @PiyushParashar i don't know there are chances to increase time in future.. i cannot really say a proper time.. different input different time –  Nov 20 '14 at 04:17
  • @Piyush Parashar I got it..please can you help me with how to close my spalsh screen when it shows a dupilcaite file present.. or before my message box.. Please have a look to my edited question.. –  Nov 20 '14 at 17:41
  • You can hide the splash screen in the RunWorkerCompleted method. This will be called after DoWork is finished. Your worker_DoWork should have the call to ExportInExcel. This is the one which might take time and you want to take it in another thread. In Button_Click just show the splash screen and in RunWorkerCompleted you can hide it. – Piyush Parashar Nov 21 '14 at 02:31

2 Answers2

0

A basic Windows application runs on a single thread usually referred to as UI thread.

You could use the BackgroundWorker. It's doing work on a separate thread from the GUI.

This object is designed to simply run a function on a different thread and then call an event on your UI thread when it's complete.

There are few articles available out side : Example Project & Multithreading

Community
  • 1
  • 1
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
  • thanks for the example.. i found lot of examples. out there in google but i don't know how to interact with my function.. that my sad point –  Nov 20 '14 at 04:27
  • First you need to understand the given example. Start from `button click` and go step by step through debugging the functions. After understanding the concept, think about interaction with your UI. – RajeshKdev Nov 20 '14 at 04:32
  • @please see my updated codes.. i tested this `BackgroundWorker` already.. where i am able to run a second from from main form.. and close it after a minute.. but i don't now how to interact with my function .. could you please update ur answer –  Nov 20 '14 at 04:34
0

I would suggest use the combination of BackgroundWorker and a ProgressBar. Since you do not know if the processing can increase in future or not, its better to do it on a separate thread so that if the user tries to interact with the parent form after clicking the button it does not freeze.

You can look at the following articles on how to use a ProgressBar control and BackgroundWorker:

a) ProgressBar

b) BackgroundWorker

You can add the progress bar on the same form or use a small pop up like user control to display the progress bar and text. The progress update can be send from the background worker (BackgroundWorker.ProgressChanged event). So you divide the ExportInExcel in chunks and send the update to the main form which will update the progress bar (10%....20%....30%). Once the last chuck of processing is over you can hide the progress bar from the user.

EDIT: Example added

//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.
}
Piyush Parashar
  • 866
  • 8
  • 20
  • thanks for the example.. i found lot of examples. out there in google but i don't know how to interact with my function.. that my sad issue –  Nov 20 '14 at 04:27
  • please see my updated codes.. as i am loading the splash screen for couple of seconds and then loading the form2.. using back gourd worker.. this is what i am able to do from those example.. i don't know how to call my function **Export Excel** using this background worker.. could you please update ur answer.. with a clear codes.. thanks a lot –  Nov 20 '14 at 04:33
  • 1
    your welcome. May I ask why this was unmarked as answer? Is there something else you require? – Piyush Parashar Nov 20 '14 at 16:15