开发者

How to pause a program for a few milliseconds?

开发者 https://www.devze.com 2022-12-23 00:58 出处:网络
How to pause a program for a few milliseconds using C++ managed code? I tried Sleep() but it didn\'t work when I included the winbase.h file, I got lots of compile errors!

How to pause a program for a few milliseconds using C++ managed code? I tried Sleep() but it didn't work when I included the winbase.h file, I got lots of compile errors!

for(int i=0; i<10; i++)
 {
   simpleOpenGlControl1->MakeCurrent();
  System::Threading::Thread::Sleep(100);
      theta_rotated += 2.0* 3.141592653/(double)Model->size()/10.0;

  simpleOpenGlControl1->Invalidate();
 }

private: System::Void simpleOpenGlControl1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
         {

             Gl::glMatrixMode(Gl::GL_MODELVIEW);
             Gl::glLoadIdentity();
             Gl::glClearColor(1, 1, 1, 1);
             Gl::glClear(Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEP开发者_运维知识库TH_BUFFER_BIT);
             camera->Tellgl();
             Gl::glRotated(Angle,X,Y,Z);
             RenderModels();
         }
void RenderModels(void)
{
    Points = gcnew System::Collections::Generic::List<Point>();
    Point p(0,0) ;
    int t = -5;
    double r = 5;
    double x, z, theta;
    for(int i = 0;i < Model->size();i++)
    {
        theta = i*2* 3.141592653/ (double)Model->size();
        x = r*sin(theta + theta_rotated);
        z = r*cos(theta + theta_rotated);
         Gl::glPushMatrix();
         Gl::glTranslated(x,0,z);
         Model->at(i).Render();
         p.X=x;
         p.Y=0;

         Points->Add(p);
         t +=1;
         Gl::glPopMatrix();
    }
    //simpleOpenGlControl1->Invalidate();
}


You want System::Threading::Thread::Sleep(). http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx


System::Threading::Thread::Sleep() as mentioned in another answer. But let me warn you, it is not precise, and for small (milliseconds) Sleep is extremely imprecise. consider that your app run together with other apps and threads and they all want processor time.


#include <unistd.h>

main() {    
   usleep(2000); // 2 msecs 
}

Read the man pages ;-) man 3 usleep

0

精彩评论

暂无评论...
验证码 换一张
取 消