C # class and a progress bar of the current thread or multi-thread calls the examples show

C # class and a progress bar of the current thread or multi-thread calls the examples show

Progress bar class:

class ShowProgress
    {
        Form pf;
        ProgressBar pb;
        Label lbl;
        public static Thread thread;
        public ShowProgress(bool isCircle)
        {
            if (isCircle)
            {
                pf = new Form();
                pf.ControlBox = false;
                pf.ShowInTaskbar = false;
                pf.TopMost = true;
                pf.FormBorderStyle = FormBorderStyle.None;
                pf.Size = new Size(600, 100);
                pb = new ProgressBar();
                Control.CheckForIllegalCrossThreadCalls = false;
                pb.Style = ProgressBarStyle.Marquee;
                pf.Controls.Add(pb);
                pf.Padding = new Padding(5);
                pb.Dock = DockStyle.Fill;
                
                pf.StartPosition = FormStartPosition.CenterScreen;
                pf.Show();
            }
            else
            {
                pf = new Form();
                pf.ControlBox = false;
                pf.ShowInTaskbar = false;
                pf.TopMost = true;
                pf.FormBorderStyle = FormBorderStyle.None;
                pf.Size = new Size(600, 100);
                pb = new ProgressBar();
                pb.Step = 1;
                pb.Maximum = 100;
                pb.Minimum = 1;                
                pf.Padding = new Padding(5);
                //pb.Dock = DockStyle.Fill;
                pb.Size = new Size(600, 80);
                lbl = new Label();
                lbl.TextAlign = ContentAlignment.MiddleCenter;
                lbl.AutoSize = false;
                lbl.Size = new Size(600, 20);   
                lbl.ForeColor = Color.Blue;
                lbl.Font = new Font("宋体", 12);
                lbl.Location = new Point(0 ,80 );
                pf.Controls.Add(lbl);
                pf.Controls.Add(pb);
                pf.StartPosition = FormStartPosition.CenterScreen;
                pf.Show();
                //pb.Value = 50;
            }
        }
        public static void run(object sp)
        {
            Random r = new Random();
            ShowProgress spp = sp as ShowProgress;
            while (true)
            {
                spp.set(r.Next(100));
                Thread.Sleep(100);
            }
        }
        public static void show_circle_dialog()
        {
            if (thread != null && thread.IsAlive)
            {
                MessageBox.Show("已经打开了一个进度,现在立即将其退出");
                try
                {
                    thread.Abort();
                }
                catch { }
                finally
                {
                    thread = null;
                }
            }
            ThreadStart ts = new ThreadStart(ShowProgress.show);
            thread = new Thread(ts);
            thread.Start();
            Thread.Sleep(200);
        }
        public static void close_circle_dialog()
        {
            if (thread != null)
            {
                try
                {
                    thread.Abort();
                }
                catch { }
                finally
                {
                    thread = null;
                }
            }
        }
        public static void show()
        {
            ShowProgress sp = new ShowProgress(true);
            run(sp);
        }
        public delegate void Set(int value);
        public delegate void deg_set();
        public void set(int value)
        {
            if (pb.InvokeRequired)
            {
                Set set_deg = new Set(set);
                pb.Invoke(set_deg, new object[] { value });
            }
            else
            {
                try { pb.Value = value; }
                catch { }
            }
        }
        public void set(int value,string pro)
        {
            if (pb.InvokeRequired)
            {
                Set set_deg = new Set(set);
                pb.Invoke(set_deg, new object[] { value });
            }
            else
            {
                try { 
                    pb.Value = value;
                    lbl.Text = pro;
                }
                catch { }
            }
        }
        public void close()
        {
            if (pf.InvokeRequired)
            {
                deg_set set_deg = new deg_set(close);
                pf.Invoke(set_deg);
            }
            else
            {
                pf.Close();
                pf.Dispose();
            }
        }
    }

Ordinary call:

ShowProgress sp = new ShowProgress(false);      
sp.set(10,"10%,完成加载【基本信息】");//完成加载基本信息
Application.DoEvents();
rules = api.get_packet_filter();
init_list();
sp.set(20,"20%,完成加载【规则】");//完成加载规则
Application.DoEvents();

Call a cycle of progress bar

ShowProgress.show_circle_dialog();
            for (int i = 1; i <= 100; i++)
            {
                Thread.Sleep(100);                
            }
ShowProgress.close_circle_dialog();         
Published 48 original articles · won praise 3 · views 20000 +

Guess you like

Origin blog.csdn.net/chscomfaner/article/details/103729793