kousikmail 0 Newbie Poster

Help me please. I am using a BackGroundWorker but i dont wanna use Thread.Sleep() Method.Is there any other options to slow down the process
here is the code..........

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Control14_12_11
{
    public partial class frmBW : Form
    {   
        #region
        public frmBW()
        {
            InitializeComponent();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted += new   RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);            
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 1000000; i++)
            {
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    //System.Threading.Thread.Sleep(1); I dont wanna use this....
                    bw.ReportProgress((i));
                }
            }
        }

        private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            listBox1.Items.Add(e.ProgressPercentage);           
            int per=((e.ProgressPercentage * 100) / 1000000);            
            progressBar1.Value = per;
            this.lblProgress.Text = "Completed : " + per + " %";
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.lblProgress.Text = "Canceled";
            }
            else if (!(e.Error == null))
            {
                this.lblProgress.Text = "Error! " + e.Error.Message.ToString();
            }
            else
            {
                progressBar1.Hide();
                lblProgress.Text = "Done!";
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            MessageBox.Show(txtShowText.Text);
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (bw.WorkerSupportsCancellation)
            {
                progressBar1.Hide();
                bw.CancelAsync();
            }
        }

        private void frmNextForm_Click(object sender, EventArgs e)
        {
            if (!bw.IsBusy)
            {
                listBox1.Items.Clear();
                progressBar1.Show();
                progressBar1.Value = 0;
                lblProgress.Visible = true;
                bw.RunWorkerAsync();
            }
        }
        #endregion

        
    }
}
zeroliken commented: Don't hijack another person's thread, start your own +0
jwenting commented: rubbish +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.