Saturday, 8 March 2014

Progress Bar Race Using Thread C#.NET

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;

namespace thread
{
    public partial class Form1 : Form
    {
     
        public Form1()
        {
            InitializeComponent();
        }
       

        bool flag=true;
       
        public void prog1()
        {
            //Change Property of progress bar
            //min=1
            //max=50000
            //size=127, 23

            for (int i = 1; i <= 50000; i++)
            {
                if (flag)
                {
                    progressBar1.Value = i;
                    textBox1.Text = i.ToString();
                    //Uncomment below code for simple progress bar

                    //if (progressBar1.Value == 100)
                    //{
                    //  MessageBox.Show("Progress Bar1 done");
                    //break;

                    //Uncomment above code for simple progress bar
                }
                else
                {
                 
                    break;
                }
            }
            flag=false;
         
        }
        public void prog2()
        {
            //Change Property of progress bar
            //min=1
            //max=50000
            //size=127, 23

            for (int i = 1; i <= 50000; i++)
            {
                if (flag)
                {
                    progressBar2.Value = i;
                    textBox2.Text = i.ToString();
                    //Uncomment below code for simple progress bar

                    //if (progressBar2.Value == 100)
                    //{
                    //    MessageBox.Show("Progress Bar2 done");
                    //    break;

                    //Uncomment above code for simple progress bar
                }
                else
                {
                   
                    break;
                }
               
            }
            flag=false;
        }
           

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th1 = new Thread(prog1);
            Thread th2 = new Thread(prog2);

         
            th1.Start();
            th2.Start();
           
        }
    }
}

No comments:

Post a Comment