Exemplo n.º 1
0
        /// <summary>
        /// Worker thread.
        /// </summary>
        ///
        public void WorkerThread()
        {
            // AVI reader
            try
            {
                // open file
                aviReader.Open(source);

                // stop positions
                int stopPosition = aviReader.Start + aviReader.Length;

                while (true)
                {
                    // start time
                    DateTime start = DateTime.Now;

                    // get next frame
                    Bitmap bitmap = aviReader.GetNextFrame();

                    framesReceived++;

                    // need to stop ?
                    if (stopEvent.WaitOne(0, false))
                    {
                        break;
                    }

                    if (NewFrame != null)
                    {
                        NewFrame(this, new NewFrameEventArgs(bitmap));
                    }

                    // free image
                    bitmap.Dispose();

                    // check current position
                    if (aviReader.Position >= stopPosition)
                    {
                        break;
                    }

                    while (this.Pause && (stopEvent.WaitOne(0, true) == false))
                    {
                        Thread.Sleep(1000);
                        start.AddSeconds(1.0);
                    }
                    // frame interval
                    int interval = (frameIntervalFromSource) ? (int)(1000 / aviReader.FrameRate) : frameInterval;
                    if (this.Speed > 1)
                    {
                        interval = (interval * 1000 / this.Speed) / 1000;
                    }
                    if (this.Speed < -1)
                    {
                        interval = interval * (0 - this.Speed);
                    }
                    // wait for a while ?
                    if (interval > 0)
                    {
                        // get frame extract duration
                        TimeSpan span = DateTime.Now.Subtract(start);

                        // miliseconds to sleep
                        int msec = interval - (int)span.TotalMilliseconds;

                        while ((msec > 0) && (stopEvent.WaitOne(0, true) == false))
                        {
                            // sleeping ...
                            Thread.Sleep((msec < 100) ? msec : 100);
                            msec -= 100;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                // provide information to clients
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(exception.Message));
                }
            }

            aviReader.Close();
            //aviReader.Dispose();
            //aviReader = null;
        }
Exemplo n.º 2
0
 public void CloseReader()
 {
     reader.Close();
 }