在C ++/CLI中,如何使事件以固定的时间间隔发生而没有Sleep() [英] How To Make an event occur in fixed time intervals without Sleep( ) in C++/CLI

查看:57
本文介绍了在C ++/CLI中,如何使事件以固定的时间间隔发生而没有Sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望进度条在特定时间间隔后具有某些值.但睡眠"功能可阻止ui出现.

我的代码

I want the progress bar to have certain values after specific time intervals. but the Sleep function Stops the ui from appearing.

My Code

private: System::Void ProgressBarSave_Load(System::Object^  sender, System::EventArgs^  e) 
	        {
		        for (int i = 0; i < 4; i++)
			{
				Thread::Sleep(3000);
				progressBar1 -> Value +=25;	//progress Bar Value
				if ( i == 3)
				{
		                    Close();			//Closes The Dialog
				}
			}
		}

推荐答案

嗯.
我从哪里开始?
如果您希望进度条显示进度,那么休眠线程总是很愚蠢的举动.首先,因为它阻止了UI线程实际更改显示,其次,因为它不允许实际制作任何程序,除非您在另一个线程上工作.而且,如果您在其他线程上运行它,那么为什么要报告进度却好像不是呢?

有两种可能:将进度条设置为跑马灯,在这种情况下,它将自动旋转而无需您干预,或者使用BackgroundWorker和ReportProgress正确完成工作
Um.
Where do I start?
If you want a progress bar to show progress, then sleeping the thread is always going to be a silly move. Firstly because it stops the UI thread from actually changing the display, and secondly, because it doesn''t allow any program to actually be made unless you have that working on a different thread. And if you have it working on a different thread, then why are you reporting progress as if it wasn''t?

There are a couple of possibilities: make the progress bar a Marquee in which case it will rotate itself without your intervention, or do the job properly with a BackgroundWorker and ReportProgress


使用 Timer class [

这篇关于在C ++/CLI中,如何使事件以固定的时间间隔发生而没有Sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆