积极答复者
进度条和线程的暂停...

问题
-
//这是在主窗口中, //Show the ProgressBar form. void ProgressBarFormShow() { _frmProgressBar = new FormProgressbar(); _incProBar = _frmProgressBar.IncreaseProbarVal; _frmProgressBar.ShowDialog(); //treat the ProgressBar as modal dialog. } //Management the ProgressBar form. void MngProgressBarForm() { try { MethodInvoker proBarFormInvo = ProgressBarFormShow; this.BeginInvoke(proBarFormInvo); Thread.Sleep(100); //sleep for a while to show the window. bool bIncProBar = false; //determine whether increase the ProgressBar successfully. do { Thread.Sleep(5); //sleep a while each time trying to increase the ProgressBar value. bIncProBar = (bool)this.Invoke(_incProBar, new object[] { 2 }); } while(bIncProBar); } catch(System.Exception ex) { MessageBox.Show(ex.Message); } } //进度条窗口中, //the method in the ProgressBar Form: /// <summary> /// increase the progressbar value. /// </summary> /// <param name="val">the val to increase each time.</param> /// <returns>true if it increase successfully;otherwise false.</returns> public bool IncreaseProbarVal(int val) { if(probar.Value + val < probar.Maximum) { probar.Value += val; return true; } else { probar.Value = probar.Maximum; return false; } }
//是这样的,主窗口中, Thread.Sleep(100); //sleep for a while to show the window. 改成Thread.Sleep(1000);就可以了... 何解?
答案
-
你是这样的:
1)你的MngProgressBarForm肯定是子线程调用的,因此Thread的Sleep必定是子线程。
2)至于为啥可以绘制界面,绘制界面是你内部又调用了this的BeginInvoke(this是窗体,BeginInvoke是对于主窗体的线程而言,内部又开辟了一个基于CLR的子线程,不同于Thread!)
3)因此你是“子线程+异步”。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 JcsonSharpenden 2013年6月25日 6:15
全部回复
-
Thread.Sleep(100)可能BeginInvoke尚未完全处理完毕,我建议你:
IAsyncResult result = this.BeginInvoke(proBarFormInvo); while(!result.IsComplete) { Thread.Sleep(100); } //Do what you want.
或者干脆同步:
this.EndInvoke(this.BeginInvoke(proBarFormInvo));
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
对呀,Thread.Sleep()用于哪个方法内,这个方法被哪个进程(线程)调用,就是Block那个线程。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
没有关系,恐怕你误会我的意思了。
Thread的Sleep方法在哪个方法中调用,该方法属于哪个线程,Sleep自然就屏蔽哪个线程。
显然你MngProgressBarForm被一个Thread调用,明显Sleep屏蔽的是这个子线程。
因为你在方法中使用了this.BeginInvoke,this显然是主窗体的UI线程,当然可以操作任意的UI上的窗体控件。
我建议看看我写的这篇博文:使用委托的BeginInvoke方法来完成复杂任务的操作
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeartModerator 2013年6月24日 1:18 补充回答
- 已建议为答案 Mike FengModerator 2013年6月24日 13:33
-
晕...两次你回答的都不一样哈...按照你 的博文,应该是 Thread Sleep的是主线程,因为如果异步的 BeginInvoke的后台线程如果没有结束,那主线程不是要 "阻塞,等待"后台线程么...怎么又是子线程哈....晕...
况且,如果是阻塞子线程的话,那
/此处Thread.Sleep(10)不可以省略,否则后台线程没有机会得到执行 while (!r.IsCompleted) { Thread.Sleep(10); } //此处不可以省略,因为任何后台线程依附于主线程。一旦主线程停止以后后台线程自动关闭。
(你的博文)..其实"高级编程"和这个写的差不多...
本来有点清晰点...你几次说的不一样...好晕哈...
-
晕...两次你回答的都不一样哈...按照你 的博文,应该是 Thread Sleep的是主线程,因为如果异步的 BeginInvoke的后台线程如果没有结束,那主线程不是要 "阻塞,等待"后台线程么...怎么又是子线程哈....晕...
况且,如果是阻塞子线程的话,那
/此处Thread.Sleep(10)不可以省略,否则后台线程没有机会得到执行 while (!r.IsCompleted) { Thread.Sleep(10); } //此处不可以省略,因为任何后台线程依附于主线程。一旦主线程停止以后后台线程自动关闭。
(你的博文)..其实"高级编程"和这个写的差不多...
本来有点清晰点...你几次说的不一样...好晕哈...
当然不一样,你根据情况而定:
子线程如果设置为BackGround,那么它依附于主线程。主线程结束子线程无论有没有执行完毕当然也就结束了。所以主线程可以通过延时或者循环判断IsCompleted的方式判断子线程是否执行完毕,以确保它的确执行完毕了方可。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
"Thread的Sleep方法在哪个方法中调用,该方法属于哪个线程,Sleep自然就屏蔽哪个线程。"这个说的貌似是"后台线程(即用BeginInvoke"产生的异步线程>
但是,貌似你博文说的,是主线程...我调试的时候,发现...Thread.Sleep中不是后台线程....那应该是主线程哦....
我的例子中当然是主线程,但是我早就一开始说过——Thread.Sleep究竟哪个线程取决于它这个方法究竟被哪个线程调用,如果是主线程使用,那么就是主线程;否则就是子线程。If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
谢谢.我有点疑问的是,我在 Button的 Click的时候:
new
Thread(MngProgressBarForm).Start();
那这个线程是子线程吧?那"
Thread.Sleep究竟哪个线程取决于它这个方法究竟被哪个线程调用"...何解?
子线程。If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
你是这样的:
1)你的MngProgressBarForm肯定是子线程调用的,因此Thread的Sleep必定是子线程。
2)至于为啥可以绘制界面,绘制界面是你内部又调用了this的BeginInvoke(this是窗体,BeginInvoke是对于主窗体的线程而言,内部又开辟了一个基于CLR的子线程,不同于Thread!)
3)因此你是“子线程+异步”。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 JcsonSharpenden 2013年6月25日 6:15