public VideoCaptureByJPEGInstance(VideoCaptureByJPEGDefinition theDefinition, TestExecution testExecution)
     : base(theDefinition, testExecution)
 {
     mCamera               = theDefinition.Camera;
     mFrameTimeout         = theDefinition.FrameTimeout;
     mStopConditionTimeout = theDefinition.StopConditionTimeout;
     mCapturedVideo        = new GeneratedVideoInstance(theDefinition.ResultantVideo, testExecution);
 }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            mHttpCameraUser.mWorkerLogMessage = "snapshot worker kicked off"; mHttpCameraUser.mWorkerLogMessageAvailable = true;

            VideoCaptureByJPEGDefinition defObject = (VideoCaptureByJPEGDefinition)mHttpCameraUser.Definition();

            int readSize   = 1024;
            int bufferSize = defObject.bufferSize;

            byte[]         buffer   = defObject.bufferForWorker;
            HttpWebRequest request  = null;
            WebResponse    response = null;
            Stream         stream   = null;
            Random         randomNumberGenerator = new Random((int)DateTime.Now.Ticks);

            HttpRequestCachePolicy bypassCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.BypassCache);

            try
            {
                while (true) // loop "indefinitely" grabbing frames until whomever started this worker(thread) cancels it
                {
                    string uri = mHttpCameraUser.Camera.CompleteImageRequestURL();
                    if (mHttpCameraUser.Camera.ProxyCacheProtection)
                    {
                        uri += ((uri.IndexOf('?') == -1) ? '?' : '&') + "proxyprevention=" + randomNumberGenerator.Next().ToString();
                    }
                    request             = (HttpWebRequest)WebRequest.Create(uri);
                    request.CachePolicy = bypassCachePolicy;
                    request.Timeout     = mHttpCameraUser.FrameTimeout;

                    if (mHttpCameraUser.Camera.Login != null && mHttpCameraUser.Camera.Login.Length > 0)
                    {
                        if (mHttpCameraUser.Camera.Password == null)
                        {
                            mHttpCameraUser.Camera.Password = String.Empty;
                        }
                        request.Credentials = new NetworkCredential(mHttpCameraUser.Camera.Login, mHttpCameraUser.Camera.Password);
                    }

                    // setting ConnectionGroupName to make sure we don't run out of connections amongst all the cameras (see http://msdn2.microsoft.com/en-us/library/ms998562.aspx section: "Connections")
                    request.ConnectionGroupName = mHttpCameraUser.TestSequence().Name + " : " + mHttpCameraUser.Name; // a unique group for each HTTP user within each TestSequence. We don't want to create unique groups every request because that would be inefficient
                    // TODO: check maxconnection attribute in Machine.config (limits the number of concurrent outbound calls)

                    ///////////
                    response = request.GetResponse();
                    stream   = response.GetResponseStream();

                    long contentLength = response.ContentLength;

                    int bytesRead      = 0;
                    int totalBytesRead = 0;

                    //ReportProgress(0, "Requesting image");
                    bool done = false;
                    while (!done)
                    {
                        if (CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        if (totalBytesRead > bufferSize - readSize)
                        {
                            e.Result = ResultCodes.BufferTooSmall;
                            return;
                        }

                        bytesRead = stream.Read(buffer, totalBytesRead, readSize);
                        if (bytesRead == 0)
                        {
                            done = true;
                        }
                        else
                        {
                            totalBytesRead += bytesRead;
                            //ReportProgress((int)(bytesRead * 100 / contentLength), "Downloading...");
                        }
                    }

                    mImage = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, totalBytesRead));
                    mHttpCameraUser.ProcessNewImage(mImage);
                }
                // we never get here since the while loop never terminates...we exit the loop on worker cancellation (above) or an exception (below)
            }
            catch (WebException exception)
            {
                switch (exception.Status)
                {
                // http://msdn2.microsoft.com/en-us/library/system.net.webexceptionstatus(vs.80).aspx
                case WebExceptionStatus.Timeout:
                    e.Result = ResultCodes.Timeout;
                    break;

                default:
                    e.Result = ResultCodes.Unspecified;
                    break;
                }
            }
            catch (Exception exception)
            {
                e.Result = ResultCodes.NonWebException;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream = null;
                }
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }