-3

I have made a method and i wanted it to put it on Backgroundworker with a progressbar in it. this is the first that im working on a Background worker.

here is my Code:

    public void WorkLoad()
    {
        string conStr, sheetName;
        conStr = string.Empty;
        //Get the name of the First Sheet.

        using (OleDbConnection kuneksyon = new OleDbConnection(Excel07ConString))
        {
            using (OleDbCommand utos = new OleDbCommand())
            {
                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {
                    utos.Connection = kuneksyon;
                    kuneksyon.Open();
                    DataTable dtExcelSchema = kuneksyon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    kuneksyon.Close();
                    DataTable dt = new DataTable();
                    utos.Connection = kuneksyon;
                    utos.CommandText = "SELECT [Engineer],[SITEID],[SITE NAME],[2G TX Status],[3G TX Status],[WO Status-LTE]  From [" + sheetName + "]";
                    kuneksyon.Open();
                    oda.SelectCommand = utos;
                    oda.Fill(dt);
                    kuneksyon.Close();
                    //Populate DataGridView.
                    ForIssuanceView.DataSource = dt;
                    ForIssuanceView.Columns.Add(" ", " ");
                }
            }
        }
    }
  • 3
    We're not writing the code for you. Do you have a more-specific question? What research have you done so far? – rory.ap Mar 07 '17 at 13:04
  • i have done some research sir, but i dont understand how to use the Backgroundworker.Dowork, ProgressChange and the RunworkerChange. I wanted someone to explain it to me . and i will apply it to my code . – Leynad Tabije Mar 07 '17 at 13:17
  • Possible duplicate: http://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker – marksfrancis Mar 07 '17 at 13:39

1 Answers1

2

Let's assume you have your background worker as a class member:

private BackgroundWorker bw;

When you are going to use it, you create and initialize it:

        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.ProgressChanged += ProgressChanged;
        bw.DoWork += DoWork;

Then you start it:

        bw.RunWorkerAsync();

You should provide a method to do actual work:

    private static void DoWork(object sender, DoWorkEventArgs e)
    {
        // do your actual work and report percentage whenever you find appropriate
        for (var p = 0; p < 100; p++)
        {
            bw.ReportProgress(p);
        }
    }

You might also provide a method to handle percentage change. It will be called automatically whenever you do ReportProgress on your background worker. Be careful, it gets launched in its own thread, not on your UI thread as you might expect:

    private static void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // inform the UI that the percentage has been changed
    }
rs232
  • 1,248
  • 8
  • 16