[vtkusers] Help with MultiThreading

John Platt jcplatt at lineone.net
Fri Nov 5 04:05:43 EST 2004


Peter,

You can try running the animation in the main thread and after rendering
each frame call the following function to let the UI update itself and
check if the stop button has been clicked.

CPafVuView::eAnimationState CPafVuView::GetAnimationState()
//
------------------------------------------------------------------------
-----
//
// Check for interaction with the animation play options.
//
//
------------------------------------------------------------------------
-----
{
   if ( m_AnimationState == AS_NONE )
      return AS_NONE;

   // Check the message queue for any waiting messages. Note that
PeekMessage(),
   // unlike GetMessage(), does not wait for a message to be placed in
the queue
   // before returning. Specifying a NULL window handle allows messages
to be
   // retrieved for ANY window that belongs to the current thread making
the
   // call (it does not retrieve messages for windows that belong to
other
   // threads).
   
   MSG msg;
   while ( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) 
   { 
      // Perform normal CWinThread message translation and dispatching.
This
      // also handles messages sent to the parent window. If the parent
is a
      // dialog, this is particularly useful since it allows the dialog
controls
      // to be updated (CDialog does not support idle time UI update).

      if ( ! AfxGetThread()->PumpMessage( ) ) 
      { 
         int nExitCode = 0;
         ::PostQuitMessage( nExitCode ); 
         break; 
      } 
   }

   // Simulate MFC's idle processing mechanism. Note that the count is
   // normally used to prioritise different kinds of idle processing.

   LONG lIdle = 0;
   while ( AfxGetApp()->OnIdle( lIdle++ ) );  

   // Check if the animation has been stopped.
   if ( m_AnimationState == AS_STOP )
      return AS_STOP;

   return AS_PLAY;
}

HTH

John.

-----Original Message-----
From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On
Behalf Of Peter Wallin
Sent: 04 November 2004 12:45
To: vtkusers at vtk.org
Subject: [vtkusers] Help with MultiThreading 

Hello there experts!,

I have an animation method in my vtk application. Its called be a simple
"play" button but now I want to be able to use a "stop" botton to stop
it.

I tried to run the animation on a seperate thread. But I dont get it to
work. It doesnt seem to listen for my button clicks. Im not sure if I
use it the right way.I only found a single example in
(vtkFiniteDifferenceGradientEstimator.cxx) on how to use
vtkMultiThreader.

Im using MFC and have implememted my vtk Code into the ViewDoc.
Below is my threading function and the functions that are linked with
the buttons.
By pressing the buttons one just set a varibale to 1 or 0, and I want
the thread to run the animation as long as the value is 1 and that it
stops when its 0. 

I would really be happy with any help because I have been stuck on this
for a long while! 

Regards
Peter


///////////////////////////////////
vtkView.cpp
////////


static VTK_THREAD_RETURN_TYPE Simulation( void *arg) // Single
{ 

sim = ((vtkViewDoc *)arg)->simulation;
steps = ((vtkViewDoc *)arg)->timesteps;

while(sim) {

for (int i=0;i<=steps;i++) {

		//updating updateVectorScaleFactor and
updateScalarScaleFactor....
               
		((vtkView
*)arg)->warp->SetScaleFactor(updateVectorScaleFactor);
		((vtkView
*)arg)->scalars->SetScaleFactor(updateScalarScaleFactor);
		((vtkView
*)arg)->actor->SetMapper(((vtkView*)arg)->pMapper);
		((vtkView *)arg)->renWin->Render();
	}

}
  return VTK_THREAD_RETURN_VALUE;

}




//Methods linked with play and stop button.


void VtkView::StartAnim() {   
multiThreader->SetSingleMethod( Simulation,(CVtkSDIView *) this );

multiThreader->SingleMethodExecute();
simulation=1;

}

void VtkView::StopAnim() {

simulation=0;

}


 

_______________________________________________
This is the private VTK discussion list. 
Please keep messages on-topic. Check the FAQ at:
<http://public.kitware.com/cgi-bin/vtkfaq>
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers





More information about the vtkusers mailing list