问题: 后台处理大量数据,要求主界面不卡死,显示进度条,且主程序体在数据处理完毕后继续运行。
1 { 2 //主程序1 3 } 4 //插入代码 5 Thread t1 = new Thread(StartWork); 6 t1.IsBackground = true; 7 t1.Start(); 8 finishHandler= 9 delegate(){ 10 //主程序2 11 //此处可能会报错:调用了不是此线程创建的……blabla 12 //使用this.Invoke(相关代码委托); 13 } 14 15 ///////////////////////////////////////////////// 16 //委托相关 17 private delegate void PRoManager(); 18 private delegate void OnFinished(); 19 private OnFinished finishHandler; 20 21 private void StartDB3Work(Object o) 22 { 23 PRoManager prostart = new PRoManager (startPro); 24 progress.Invoke(prostart); 25 26 {27 //数据处理代码 28 } 29 if (finishHandler!=null) 30 finishHandler(); 31 } 32 33 //progressBar 相关代码 34 System.Windows.Forms.Timer timer; 35 private void startPro() 36 { 37 progress.Minimum = 0; 38 progress.Maximum = 100; 39 progress.Value = 0; 40 41 timer = new System.Windows.Forms.Timer(); 42 timer.Tick += new EventHandler(tPro_Tick); 43 timer.Start(); 44 } 45 private void tPro_Tick(object sender,EventArgs e) 46 { 47 int iNum = progress.Value; 48 if (iNum != 100) 49 { 50 progress.Increment(4); 51 } 52 else 53 { 54 progress.Value = 0; 55 } 56 } 57 private void closePro() 58 { 59 if(timer!= null) 60 { 61 timer.Stop(); 62 timer.Dispose(); 63 timer = null; 64 } 65 progress.Value = 100; 66 }
时间: 2024-11-05 02:21:48