[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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

0
casit
Asked:
casit
  • 4
1 Solution
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now