C#进度条

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37137902/article/details/88896305

    public partial class TransmissionSchedule : Form
    {
        public double CompletedNumber = 0;
        public double AllNumber = 0;

        public byte[] buffer = new byte[1024 * 1024 * 1024];
        public FileStream fsRead;
        public Socket SocketAccept;

        Thread fThread;
        public TransmissionSchedule()
        {
            InitializeComponent();
            this.progressBar1.Minimum = 0;
            this.progressBar1.Maximum = 100;
        }

        private delegate void SetPos(double ipos, string vinfo);//代理
        private void SetTextMesssage(double ipos, string vinfo)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMesssage);
                this.Invoke(setpos, new object[] { ipos, vinfo });
            }
            else
            {
                this.label1.Text ="进度: "+ ipos.ToString() + "%";
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.textBox1.AppendText(vinfo);

                if (ipos == 100)
                {
                    Thread.Sleep(1000);
                    fThread.Abort();                  
                    MessageBox.Show("传输完成!");
                    this.Close();
                }
            }
        }

        private void SleepT()
        {
            double send = 0; //发送的字节数 
            double length = fsRead.Length;
            while (true)  //大文件断点多次传输
            {
                int r = fsRead.Read(buffer, 0, buffer.Length);
                if (r == 0)
                {
                    break;
                }
                if(buffer.Length!=0)
                {
                    SocketAccept.Send(buffer, 0, r, SocketFlags.None);
                }
                send += r;

                if (send != 0 && length != 0)
                {
                    double i = Math.Round(send / length, 2);
                    SetTextMesssage(Math.Round(send / length * 100, 2), send.ToString() + "\r\n");
                }
            }
        }

        public void start()
        {
            //SleepT();
        }

        private void TransmissionSchedule_Load(object sender, EventArgs e)
        {
            Thread.Sleep(20);
            fThread = new Thread(new ThreadStart(SleepT));
            fThread.IsBackground = true;
            fThread.Start();
        }
    }

猜你喜欢

转载自blog.csdn.net/m0_37137902/article/details/88896305