/// <summary>
 /// Método acionado quando o download for completado.
 /// </summary>
 /// <param name="error"></param>
 /// <param name="cancelled"></param>
 /// <param name="result"></param>
 protected void OnDownloadCompleted(Exception error, bool cancelled, DataEntryPackage result)
 {
     if (DownloadCompleted != null)
     {
         DownloadCompleted(this, new DataEntryDownloadCompletedEventArgs(error, cancelled, null, result));
     }
 }
        private void DoWork(object userState)
        {
            DataEntryPackage result        = null;
            Exception        lastException = null;

            try
            {
                var              versions  = _versions.ToArray();
                string           name      = null;
                System.IO.Stream outStream = null;
                System.IO.Stream fsStream  = null;

                var totalBytesToReceive = Client.GetDataEntries(versions, out name, out outStream);

                try
                {
                    var buffer = new byte[1024];
                    var read   = 0;

                    var tempFile = System.IO.Path.GetTempFileName();

                    fsStream = new DownloaderFileStream(this, totalBytesToReceive, tempFile, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);

                    while ((read = outStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fsStream.Write(buffer, 0, read);
                    }

                    fsStream.Position = 0;

                    result = new DataEntryPackage(fsStream);
                }
                catch
                {
                    if (fsStream != null)
                    {
                        fsStream.Dispose();
                    }
                    throw;
                }
                finally
                {
                    outStream.Dispose();
                }
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                if (!_isCancelled)
                {
                    lastException = ex;
                }
            }
            catch (Exception ex)
            {
                lastException = ex;
            }

            _isBusy = false;

            try
            {
                // Aciona o evento informado que o download foi feito
                OnDownloadCompleted(lastException, _isCancelled, result);
            }
            finally
            {
                if (result != null)
                {
                    result.Dispose();
                }
            }
        }