Index: /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.cpp
===================================================================
--- /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.cpp	(revision 4496)
+++ /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.cpp	(revision 4497)
@@ -30,9 +30,11 @@
   : m_pReceiver(p_pReceiver), m_pImage(p_pImage), m_pDark(p_pDark)
 {
-	m_pData = m_pImage->bits();
+	m_pData = p_pImage->bits();
 	m_iScaleMax = 200;
 	m_zDarks = false;
 	m_zScale = false;
 	m_iSigmas = 15;
+	m_zIsActive = false;
+	m_zStop = false;
 
 	m_qlSpotList.setAutoDelete( true );
@@ -47,7 +49,47 @@
 void VideoSpotThread::run()
 {
-//  qDebug("VideoSpotThread PID: %d - PPID: %d", getpid(), getppid() );
-  findSpot();
-	QThread::postEvent( m_pReceiver, new QCustomEvent( THREAD_END_EVENT ) );
+  qDebug("VideoSpotThread PID: %d - PPID: %d", getpid(), getppid() );
+	m_zIsActive = false;
+	m_zStop = false;
+
+	while( true )
+	{
+		/*
+		 * Check our protected state variable to see if we should stop running
+		 */
+		m_aMutex.lock();
+		if( m_zStop )
+		{
+		  m_zIsActive = false;
+			m_aMutex.unlock();
+			return;
+		}	
+
+		/*
+		 * We wait here to be woken up to process the next frame.
+		 * This ensures that when we stop the thread and wan't do delete this class
+		 * we get woken up for sure.
+		 * Otherwise it could be that when the controlling thread sets the stop varible
+		 * and tries to wake us up to finish this loop, we would still process a frame
+		 * and will not get woken up. In this case the program would hang.
+		 */
+		m_aWaitCondition.wait( &m_aMutex );
+		if( m_zStop )
+		{
+//			qDebug("Preparing to exit: Unlocking Mutex");
+		  m_zIsActive = false;
+			m_aMutex.unlock();
+//			qDebug("Stoping  running Thread");
+			return;
+		}	
+ 		m_aMutex.unlock();
+
+	  findSpot();
+		QThread::postEvent( m_pReceiver, new QCustomEvent( THREAD_SPOT_EVENT ) );
+
+		m_aMutex.lock();
+		m_zIsActive = false;
+		m_aMutex.unlock();
+	}
 }
 
@@ -114,5 +156,5 @@
 	uchar* pData = m_pData;
 	uchar* pDark = m_pDark;
-	uchar* pEnd = m_pData + ( MY_HEIGHT * MY_WIDTH * MY_DEPTH);
+	uchar* pEnd = pData + ( MY_HEIGHT * MY_WIDTH * MY_DEPTH);
 	for( ; pData < pEnd; pData++, pDark++ )
 	{
@@ -164,2 +206,78 @@
 	return threshold;
 }
+
+void VideoSpotThread::stop()
+{
+//	qDebug("Setting stop flag");
+	m_aMutex.lock();
+	m_zStop = true;
+	m_aMutex.unlock();
+}
+
+bool VideoSpotThread::isActive()
+{
+	m_aMutex.lock();
+	bool zIsActive = m_zIsActive;
+	m_aMutex.unlock();
+
+	return zIsActive;                                                                                  		
+}
+
+void VideoSpotThread::wake()
+{
+//	qDebug("Waking up the spot thread");
+	if( running() )
+	{
+		while( true )
+		{
+			m_aMutex.lock();
+			if( ! m_zIsActive )
+				break;
+			m_aMutex.unlock();
+		}		
+//		qDebug("Setting active");
+ 		m_zIsActive = true;
+		m_aMutex.unlock();
+
+		m_aWaitCondition.wakeOne();
+	}
+
+}
+
+void VideoSpotThread::getSpotList( QList<VideoSpot>* p_pSpots )
+{
+	
+	while( true )
+	{
+		m_aMutex.lock();
+		if( ! m_zIsActive )
+			break;
+		m_aMutex.unlock();
+	}		
+	p_pSpots->clear();
+	for( VideoSpot* pActualSpot = m_qlSpotList.first();
+	  	 pActualSpot; pActualSpot = m_qlSpotList.next() )
+	{
+		p_pSpots->append( new VideoSpot( *pActualSpot ) );
+	}
+	m_aMutex.unlock();
+	return;
+};
+
+void VideoSpotThread::getFrame( uchar* p_pData )
+{
+
+	while( true )
+	{
+		m_aMutex.lock();
+		if( ! m_zIsActive )
+			break;
+		m_aMutex.unlock();
+ }		
+
+	m_aDataMutex.lock();
+	memcpy( p_pData, m_pData, m_pImage->numBytes());
+	m_aDataMutex.unlock();
+	m_aMutex.unlock();
+}
+                      	
Index: /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.h
===================================================================
--- /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.h	(revision 4496)
+++ /trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/videospotthread.h	(revision 4497)
@@ -36,4 +36,13 @@
 
   /** No descriptions */
+  bool isActive();
+
+  /** No descriptions */
+  virtual void wake();
+
+  /** No descriptions */
+  virtual void stop();
+
+  /** No descriptions */
   virtual void subtractDarks();
   /** No descriptions */
@@ -44,5 +53,7 @@
 	virtual int getThreshold();
   /** No descriptions */
-	virtual QList<VideoSpot>& getSpotList() { return m_qlSpotList; };
+	virtual void getSpotList( QList<VideoSpot>* p_pSpots );
+  /** No descriptions */
+	virtual void getFrame( uchar* p_pData );
   /** No descriptions */
   virtual void setScalingMax( int p_iVal ) { m_iScaleMax = p_iVal; };
@@ -59,4 +70,5 @@
   /** No descriptions */
   virtual void run();
+
 private: // Private attributes
   /**  */
@@ -70,4 +82,14 @@
   /**  */
 	QList<VideoSpot> m_qlSpotList;
+  /**  */
+	QMutex m_aMutex;
+  /**  */
+	QMutex m_aDataMutex;
+  /**  */
+	QWaitCondition m_aWaitCondition;
+	/** */
+  bool m_zStop;
+	/** */
+  bool m_zIsActive;
   /**  */
   bool m_zDarks;
