日期:2014-05-18 浏览次数:20967 次
private void Form1_Load(object sender, EventArgs e) { toolStripProgressBar1.Maximum = 100; toolStripProgressBar1.Value=1; while (toolStripProgressBar1.Value <100) { toolStripProgressBar1.Value = toolStripProgressBar1.Value+1; if (toolStripProgressBar1.Value == 99) { toolStripProgressBar1.Value = 1; } } }
public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } private void Form1_Load(object sender, EventArgs e) { Thread t1 = new Thread(new ThreadStart(DoRecycle)); t1.Start(); } private void DoRecycle() { pb.Maximum = 100; pb.Visible = true; pb.Value = 1; while (true) { pb.Value++; if (pb.Value == 100) { pb.Value = 1; } Thread.Sleep(100); } }
------解决方案--------------------
像楼主把写在load事件或者按钮click事件直接执行while会导致程序假死,UI主线程一直在执行你的while,跳不出这个循环,根本就没有机会去做其他的事情,想要实现这个效果要再开一个线程去处理进度条(就像16楼),或者用一个System.Windows.Forms.Timer 控件实现,建议楼主去看看多线程、同步、异步