views:

171

answers:

3

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_DEPTH_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();
}
+3  A: 

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

Billy ONeal
I am using openGL, do you think openGL uses another thread, because I feel the application gets slowed down but the rendering of the openGL does not pause!
Ahmad Farid
OpenGL is probably running on your graphics processor -- and as such is probably not pauseable in that way. But I really don't know.
Billy ONeal
A: 
#include <unistd.h>

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

Read the man pages ;-) man 3 usleep

darkturo
man pages for "managed-c++". something new
Andrey
'man' is not recognized as an internal or external command,operable program or batch file.
Bill
You know, some people would say that answers are more helpful when they refer to the same language and platform as the OP asked about... ;)
jalf
@Bill: `man` is a program on *nix systems to display information from *man pages*, most of which are help files.
Thomas Matthews
it did not work, lots of compiler errors! maybe because mixing between managed and unmanaged codes?
Ahmad Farid
@Thomas: I know. However, the OP was most likely asking about a Windows platform, and I was attempting to point that out by showing that `man 3 usleep` is not a generic solution.
Bill
ooops! I think I miss the point! hahah
darkturo
+2  A: 

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.

Andrey
I am using openGL, do you think openGL uses another thread, because I feel the application gets slowed down but the rendering of the openGL does not pause! So it there something to pause all the threads?
Ahmad Farid
opengl rendering takes place in separate thread. what is our final goal? please elaborate
Andrey
I am updating the position of some model in a for loop and would like the loop to pause for some time before updating the new position so as the model to appear as if it is moving. I am pasting some code above. Thanks.
Ahmad Farid
That is incorrect pattern for OpenGL animation. either use GLUT (google it) or check this tutorial, it shows how to handle animation correctly. http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=02
Andrey
But man I took care of that. In the pain event of the OGL control, I have pasted the code again. I just didn't want to bother you by lots of details from the beginning. Thanks though.
Ahmad Farid
`System::Threading::Thread::Sleep(100); theta_rotated += 2.0* 3.141592653/(double)Model->size()/10.0;`this is incorrect. really. please check my links. you shouln't iterate. you should take time spans between updates and apply rotation function to it.
Andrey