AForge.Robotics.Surveyor.SRV1Camera.WorkerThread C# (CSharp) Method

WorkerThread() private method

Worker thread.
private WorkerThread ( ) : void
return void
        private void WorkerThread( )
        {
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch( );

            // buffer to read stream into
            byte[] buffer = new byte[bufferSize];

            while ( !stopEvent.WaitOne( 0, false ) )
            {
                try
                {
                    stopWatch.Reset( );
                    stopWatch.Start( );

                    int bytesRead = communicator.SendAndReceive( new byte[] { (byte) 'I' }, buffer );

                    bytesReceived += bytesRead;

                    if ( bytesRead > 10 )
                    {
                        // check for image reply signature
                        if (
                            ( buffer[0] == (byte) '#' ) &&
                            ( buffer[1] == (byte) '#' ) &&
                            ( buffer[2] == (byte) 'I' ) &&
                            ( buffer[3] == (byte) 'M' ) &&
                            ( buffer[4] == (byte) 'J' ) )
                        {
                            // extract image size
                            int imageSize = System.BitConverter.ToInt32( buffer, 6 );

                            if ( !stopEvent.WaitOne( 0, false ) )
                            {
                                try
                                {
                                    // decode image from memory stream
                                    Bitmap bitmap = (Bitmap) Bitmap.FromStream( new MemoryStream( buffer, 10, imageSize ) );
                                    framesReceived++;

                                    // let subscribers know if there are any
                                    if ( NewFrame != null )
                                    {
                                        NewFrame( this, new NewFrameEventArgs( bitmap ) );
                                    }

                                    bitmap.Dispose( );
                                }
                                catch
                                {
                                }

                                // wait for a while ?
                                if ( frameInterval > 0 )
                                {
                                    // get download duration
                                    stopWatch.Stop( );

                                    // miliseconds to sleep
                                    int msec = frameInterval - (int) stopWatch.ElapsedMilliseconds;

                                    while ( ( msec > 0 ) && ( stopEvent.WaitOne( 0, false ) == false ) )
                                    {
                                        // sleeping ...
                                        Thread.Sleep( ( msec < 100 ) ? msec : 100 );
                                        msec -= 100;
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    if ( VideoSourceError != null )
                    {
                        VideoSourceError( this, new VideoSourceErrorEventArgs( "Failed receiving video frame from SRV-1." ) );
                    }
                }
            }

            stopWatch.Stop( );

            if ( PlayingFinished != null )
            {
                PlayingFinished( this, ReasonToFinishPlaying.StoppedByUser );
            }
        }           
    }