C# backgroundworker

I have code that does a web-service request. While doing this request I need a progress-bar to be moving independently.

My problem is that I just need to say run a progress update every 1 or 2 seconds and check to see if progress of the request has been completed.
NetBasisServicesSoapClient client = new NetBasisServicesSoapClient();
            TransactionDetails[] transactions = new TransactionDetails[dataGridView1.Rows.Count - 1];
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                transactions[i] = new TransactionDetails();
                transactions[i].TransactionDate = (string)dataGridView1.Rows[i].Cells[2].Value;
                transactions[i].TransactionType = (string)dataGridView1.Rows[i].Cells[3].Value;
                transactions[i].Shares = (string)dataGridView1.Rows[i].Cells[4].Value;
                transactions[i].Pershare = (string)dataGridView1.Rows[i].Cells[5].Value;
                transactions[i].TotalAmount = (string)dataGridView1.Rows[i].Cells[6].Value;
            }
            CostbasisResult result = client.Costbasis(dataGridView1.Rows[0].Cells[0].Value.ToString(), dataGridView1.Rows[0].Cells[1].Value.ToString(), transactions, false, "", "", "FIFO", true);
            string result1 = ConvertStringArrayToString(result.Details);

Open in new window

casitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gene_CypCommented:

 
Three key things:
backgroundWorker1.RunWokerAsync();

private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
   // execution code
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // recieve the return value here
    int returnValue = (int)e.Result;
    // Or for example:
   List<Myobject> myList = ( List<Myobject>)e.Result;
 
}
http://www.c-sharpcorner.com/UploadFile/Ashush/BackgroundWorker03082008094954AM/BackgroundWorker.aspx
 http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gene_CypCommented:
Sorry this should read:
 

backgroundWorker1.RunWorkerAsync();
 
You place that line where you want it to start carrying the process out.
Since you want it to start every x amount of seconds, you can use a Timer component for that with an "OnTick" event handler and set it to the amount of seconds you want it to be. Be sure to check that the previous run has finished though.
0
casitAuthor Commented:
Alright so Currently I have this.
However the dataGridView1 is now showing as red underline.


        private void btnSingle_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessMyRequest();
                //output box = new output();
                //box.Show();
                MessageBox.Show(global_vars.result,"Results",MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An Error has occured.  Please try again."+ex.Message);
            }
        }
        #region Background Work of My Request

        private void ProcessMyRequest()
        {            
            if (!bkgWorkerMyRequest.IsBusy)
            {
                toolStripStatusLabel1.Text = "Processing Request...";
                bkgWorkerMyRequest.RunWorkerAsync();
            }
        }
        private void bkgWorkerMyRequest_DoWork(object sender, DoWorkEventArgs e)
        {
            // let's process what we need in a diferrent thread than the UI thread
            string r = GetStuffDone();
            e.Result = r;
            //reports a percentage between 0 and 100
            bkgWorkerMyRequest.ReportProgress(i * 10); 
        }
        private void bkgWorkerMyRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            global_vars.result = (String)e.Result;
            toolStripStatusLabel1.Text = "Completed";
        }
        private void bkgWorkerMyRequest_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Value = e.ProgressPercentage;
        }
    #endregion

        static string GetStuffDone() 
        {
            NetBasisServicesSoapClient client = new NetBasisServicesSoapClient();
            TransactionDetails[] transactions = new TransactionDetails[dataGridView1.Rows.Count - 1];
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                transactions[i] = new TransactionDetails();
                transactions[i].TransactionDate = (string)dataGridView1.Rows[i].Cells[2].Value;
                transactions[i].TransactionType = (string)dataGridView1.Rows[i].Cells[3].Value;
                transactions[i].Shares = (string)dataGridView1.Rows[i].Cells[4].Value;
                transactions[i].Pershare = (string)dataGridView1.Rows[i].Cells[5].Value;
                transactions[i].TotalAmount = (string)dataGridView1.Rows[i].Cells[6].Value;
            }
            CostbasisResult result = client.Costbasis(dataGridView1.Rows[0].Cells[0].Value.ToString(), dataGridView1.Rows[0].Cells[1].Value.ToString(), transactions, false, "", "", "FIFO", true);
            return ConvertStringArrayToString(result.Details);
        }

Open in new window

0
Gene_CypCommented:
That's because you declared "GetStuffDone()" as "static"
 
0
Gene_CypCommented:
It would also be better if you create the DataGridView as a parameter, making the "GetStuffDone" more independent.
 
GetStuffDone(DataGridView dgv) for example.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.