예제 #1
0
        private static IEnumerator <Int32> CopyStream(AsyncEnumerator <Int64> ae, Stream source, Stream destination, Int32 bufferSize, Action <Int64> reportProgress)
        {
            Byte[] buffer         = new Byte[bufferSize];
            Int64  totalBytesRead = 0;

            while (true)
            {
                ae.SetOperationTag("Reading from source stream");
                // Read whichever is smaller (number of bytes left to read OR the buffer size)
                source.BeginRead(buffer, 0, buffer.Length, ae.End(), null);
                yield return(1);

                Int32 bytesReadThisTime = source.EndRead(ae.DequeueAsyncResult());
                totalBytesRead += bytesReadThisTime;

                ae.SetOperationTag("Writing to destination stream");
                destination.BeginWrite(buffer, 0, bytesReadThisTime, ae.End(), null);
                yield return(1);

                destination.EndWrite(ae.DequeueAsyncResult());

                if (reportProgress != null)
                {
                    reportProgress(totalBytesRead);
                }
                if (bytesReadThisTime < buffer.Length)
                {
                    break;
                }
            }
            ae.Result = totalBytesRead;
        }
예제 #2
0
      private static IEnumerator<Int32> CopyStream(AsyncEnumerator<Int64> ae, Stream source, Stream destination, Int32 bufferSize, Action<Int64> reportProgress) {
         Byte[] buffer = new Byte[bufferSize];
         Int64 totalBytesRead = 0;
         while (true) {
            ae.SetOperationTag("Reading from source stream");
            // Read whichever is smaller (number of bytes left to read OR the buffer size)
            source.BeginRead(buffer, 0, buffer.Length, ae.End(), null);
            yield return 1;
            Int32 bytesReadThisTime = source.EndRead(ae.DequeueAsyncResult());
            totalBytesRead += bytesReadThisTime;

            ae.SetOperationTag("Writing to destination stream");
            destination.BeginWrite(buffer, 0, bytesReadThisTime, ae.End(), null);
            yield return 1;
            destination.EndWrite(ae.DequeueAsyncResult());

            if (reportProgress != null) reportProgress(totalBytesRead);
            if (bytesReadThisTime < buffer.Length) break;
         }
         ae.Result = totalBytesRead;
      }
예제 #3
0
      private IEnumerator<Int32> GetPhotos(AsyncEnumerator ae, String input) {
         WebRequest request = HttpWebRequest.Create(
            new Uri(String.Format("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&tags={1}&per_page=10", 
               FlickrKey.Key, input)));
         ae.SetOperationTag("Querying " + request.RequestUri);
         request.BeginGetResponse(ae.End(), null);
         yield return 1;
         // Complete the Flickr request (this is guaranteed to run on the GUI thread)
         String data;
         using (HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(ae.DequeueAsyncResult())) {
            using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
               data = reader.ReadToEnd();
            }
         }

         ae.SetOperationTag("Starting Storyboard");
         EventApmFactory<EventArgs> eventArgsFactory = new EventApmFactory<EventArgs>();
         EventHandler storyboardCompletedEventHandler = eventArgsFactory.PrepareOperation(ae.End()).EventHandler;
         SearchPanelStoryBoard.Completed += storyboardCompletedEventHandler;
         SearchPanelStoryBoard.Begin();
         yield return 1;

         SearchPanelStoryBoard.Completed -= storyboardCompletedEventHandler;
         eventArgsFactory.EndInvoke(ae.DequeueAsyncResult());
         PhotoBoard.Width = Application.Current.Host.Content.ActualWidth;
         PhotoBoard.Height = Application.Current.Host.Content.ActualHeight - 100.0;

         XDocument doc = XDocument.Parse(data);
         var photos = from results in doc.Descendants("photo")
                      select new {
                         id = results.Attribute("id").Value.ToString(),
                         farm = results.Attribute("farm").Value.ToString(),
                         server = results.Attribute("server").Value.ToString(),
                         secret = results.Attribute("secret").Value.ToString()
                      };

         PhotoBoard.Children.Clear();
         Int32 count = 0;

         EventApmFactory<OpenReadCompletedEventArgs> openReadCompletedEventArgsFactory = new EventApmFactory<OpenReadCompletedEventArgs>();
         foreach (var photo in photos) {
            // Create a photo
            Border item = (Border) XamlReader.Load(String.Format(CultureInfo.InvariantCulture,
                c_template, count,
                (PhotoBoard.Width - c_cx) / 2, (PhotoBoard.Height - c_cy) / 2,   // Initial position (center of workspace)
                c_cx + c_border, c_cy + c_border,                                // Width and height of photo border
                (c_cx + c_border) / 2, (c_cy + c_border) / 2,                    // Center of rotation
                c_cx, c_cy));                                                    // Width and height of photo


            WebClient wc = new WebClient();
            ae.SetOperationTag("Loading photo " + count);
            wc.OpenReadCompleted += openReadCompletedEventArgsFactory.PrepareOperation(ae.End()).EventHandler;
            wc.OpenReadAsync(
               new Uri(String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_m.jpg", photo.farm, photo.server, photo.id, photo.secret), 
                  UriKind.Absolute), item);
            count++;
         }
         while (count-- > 0) {
            yield return 1;  // Process each image as it comes back
            OnReadCompleted(null, openReadCompletedEventArgsFactory.EndInvoke(ae.DequeueAsyncResult()));
         }
      }