4 Replies - 301 Views - Last Post: 28 January 2013 - 06:24 AM Rate Topic: -----

#1 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

BackgroudWorker and Threads problam

Posted 28 January 2013 - 05:38 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;


namespace App_Test
{
    public partial class Form1 : Form
    {
       

        public Form1()

        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Maximum = 100;
            backgroundWorker1.RunWorkerAsync();


        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int c = 0;

            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");

            sqlcon.Open();

            SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
            SqlDataReader reader;

            reader = com.ExecuteReader();


            while (reader.Read())
            {
                // loop thurogh 394 records

                comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0)); }));

                backgroundWorker1.ReportProgress(c += 1);

            }

            sqlcon.Close();

            e.Result = "Loading completed";


        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int pres = (int)e.ProgressPercentage;

            progressBar1.Value = pres; 
            
            // got a exception because i have 394 Artist in my database
            // progressbar can hold upto 100 



        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string msg = (string)e.Result;

            MessageBox.Show(msg);
        }
     




    }

}




What my Code will do ?

This will load artist Names into a combobox1

i have use a backgroundworker to keep track on loading bar according to artist loading into combobox

something like this

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int c = 0;

            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");

            sqlcon.Open();

            SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
            SqlDataReader reader;

            reader = com.ExecuteReader();


            while (reader.Read())
            {
                // loop thurogh 394 records

                comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0)); }));

                backgroundWorker1.ReportProgress(c += 1);

            }

            sqlcon.Close();

            e.Result = "Loading completed";


        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int pres = (int)e.ProgressPercentage;

            progressBar1.Value = pres; 
            
            // got a exception because i have 394 Artist in my database
            // progressbar can hold upto 100 



        }


i got a exception when exceed the progress bar

i have 380 Artist in my database

progress bar can hold only upto 100

how do i convert this 380 or (n number of artists) to 100% presentage

as a example

progress bar 99% full when the 379 Artist add into combobox1
progress bar 100% full when the 380 (last artist) add into combobox1

if you can come across any mathematical logic please enplane
thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: BackgroudWorker and Threads problam

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,446
  • Joined: 12-December 12

Re: BackgroudWorker and Threads problam

Posted 28 January 2013 - 05:57 AM

x * 100 / 380.0

or rounding up:

int roundedUp = (int) Math.Ceiling(x * 100 / 380.0);

Sorry, but I don't really want to explain basic maths.

You can use Min() if you want to be doubly sure (excusing the pun!) that it doesn't go above 100:

int roundedUp = (int) Min(100, Math.Ceiling(x * 100 / 380.0));

This post has been edited by andrewsw: 28 January 2013 - 06:07 AM

Was This Post Helpful? 3
  • +
  • -

#3 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

Re: BackgroudWorker and Threads problam

Posted 28 January 2013 - 06:05 AM

View Postandrewsw, on 28 January 2013 - 05:57 AM, said:

x * 100 / 380.0

or rounding up:

int roundedUp = (int) Math.Ceiling(x * 100 / 380.0);

Sorry, but I don't really want to explain basic maths.



hey hey .. thanks i will look into it and let you know sir . im really bad in math also english :D
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,446
  • Joined: 12-December 12

Re: BackgroudWorker and Threads problam

Posted 28 January 2013 - 06:10 AM

No worries. I added a little more detail to my previous post which you may have missed.
Was This Post Helpful? 0
  • +
  • -

#5 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

Re: BackgroudWorker and Threads problam

Posted 28 January 2013 - 06:24 AM

View Postistore221, on 28 January 2013 - 06:05 AM, said:

View Postandrewsw, on 28 January 2013 - 05:57 AM, said:

x * 100 / 380.0

or rounding up:

int roundedUp = (int) Math.Ceiling(x * 100 / 380.0);

Sorry, but I don't really want to explain basic maths.



hey hey .. thanks i will look into it and let you know sir . im really bad in math also english :D/>



private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            object c = 0;

            int count_in_loop = 0;

            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");

            sqlcon.Open();

            SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
            SqlCommand com2 = new SqlCommand("SELECT COUNT(Artist_Name) FROM Artists", sqlcon);

            c = com2.ExecuteScalar(); // get number of Artist 

          

            SqlDataReader reader;

            reader = com.ExecuteReader();

           
            while (reader.Read())
            {
                // loop thurogh 394 records

                comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0)); }));

                count_in_loop += 1;


                // logic is 
                // x * 100 / (number of artist)

                int roundedUp = (int)Math.Ceiling((decimal)count_in_loop * 100 / (int)c);

                backgroundWorker1.ReportProgress(roundedUp);
            }

            sqlcon.Close();

            e.Result = "Loading completed";


        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int pres = (int)e.ProgressPercentage;

            progressBar1.Value = pres;
            this.Text = pres.ToString();
            // got a exception because i have 394 Artist in my database
            // progressbar can hold upto 100 



        }


i changed my code according to your logic works like a baby thanks in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1