c#中代码中多线程动态创建progressbar的实例,概念很重要可扩展很多类似概念
以下是代碼中創建progressbar的實例
?int count = 0;
????????private void button4_Click(object sender, EventArgs e)
????????{
????????????Thread th = new Thread(() => {
????????????????Form form = new Form();
????????????????form.Name="myForm";
????????????????form.Width = 200;
????????????????form.Height = 20;
????????????????form.ControlBox = false;
????????????????//form.ShowInTaskbar = false;
????????????????form.StartPosition = FormStartPosition.CenterScreen;
????????????????ProgressBar pb = new ProgressBar();
????????????????pb.Dock = DockStyle.Fill;
????????????????pb.Maximum = 100;
????????????????pb.Minimum = 0;
????????????????pb.Value = count;
????????????????pb.BringToFront();
????????????????pb.Visible = true;
????????????????pb.Parent = form;
????????????????form.ShowDialog();
????????????
????????????});
????????????th.Start();
????????????for (int i = 0; i < 1000;i++ )
????????????{
????????????????Thread.Sleep(5);
????????????????count = Convert.ToInt32(i * 1.0 / 1000 * (100 - 0) + 0);
??????????????Control control= ?FindForm("myForm");
??????????????if (control!=null&&control.Name == "myForm" && control.IsHandleCreated)
????????????????{
????????????????????control.Invoke(new Action(() => {
????????????????????????ProgressBar pb = control.Controls[0] as ProgressBar;
????????????????????????pb.Value = count;
????????????????????}));
???????????????????
????????????????}
????????????}
????????????th.Abort();
????????}
????????public Form FindForm(string name)
????????{
????????????foreach (Form f in Application.OpenForms)
????????????{
????????????????if (f.Name == name) return f;
????????????}
????????????return null;
????????}
?
總結
以上是生活随笔為你收集整理的c#中代码中多线程动态创建progressbar的实例,概念很重要可扩展很多类似概念的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven 之 setting.xm 的
- 下一篇: c#滚动条创建实例,多线程