none
webclient 記憶體不足 要怎麼釋放內存 RRS feed

  • 问题



  • using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using QueueLoad.controls;
    using QueueLoad.ServiceReference1;
    using System.Windows.Shapes;
    using System.Windows.Media;
    using System.Windows.Media.Effects;
    
    namespace QueueLoad
    {
      /// <summary>
      /// 顺序,倒序异步加载一组图片(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
      /// </summary>
      public partial class MainPage : UserControl
      {
     
        int pageDownloads = 0;
        WebClient _wc = new WebClient();
    
        public MainPage()
        {
          InitializeComponent();
          ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
          client.getprojectimageinfoAsync("別墅外牆");
          client.getprojectimageinfoCompleted += new EventHandler<ServiceReference1.getprojectimageinfoCompletedEventArgs>(client_getprojectimageinfoCompleted);
         
        }
        Image imgItem;
        Grid grid;
        TextBlock tb;
        List<YHProject> p;
        void client_getprojectimageinfoCompleted(object sender, ServiceReference1.getprojectimageinfoCompletedEventArgs e)
        {
          p = new List<YHProject>(e.Result);
          for (int i = 0; i < p.Count; i++)
          {
            grid = new Grid();
            imgItem = new Image();
            imgItem.Width = 100;
            imgItem.Height = 70;
            imgItem.Name = "img" + (i + 1).ToString();
            imgItem.Margin = new Thickness(5, 0, 0, 0);
            //imgItem.MouseLeftButtonDown += new MouseButtonEventHandler(imgItem_MouseLeftButtonDown);
            //imgItem.MouseEnter += new MouseEventHandler(imgDown_MouseEnter);
            //imgItem.MouseLeave += new MouseEventHandler(imgDown_MouseEnter);
            Loading ld = new Loading();
            ld.VerticalAlignment = VerticalAlignment.Center;
            ld.HorizontalAlignment = HorizontalAlignment.Center;
            Rectangle r = new Rectangle();
            r.Width = 50;
            r.Height = 20;
            r.Margin = new Thickness(45, 60, 0, 0);
            r.Fill = new SolidColorBrush(Colors.Black);
            tb = new TextBlock();
            tb.Margin = new Thickness(53, 60, 0, 0);
            tb.Name = "tb" + (i + 1).ToString();
            tb.Foreground = new SolidColorBrush(Colors.White);
            grid.Children.Add(ld);
            grid.Children.Add(imgItem);
            grid.Children.Add(r);
            grid.Children.Add(tb);
            this.stkpnlImage.Children.Add(grid);
          }
    
          _wc.OpenReadCompleted += new OpenReadCompletedEventHandler(_wc_OpenReadCompleted);
          LoadImage();
        }
       
       
    
        Image im;
        void _wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
          
    
            if (e.Error == null)
            {
              BitmapImage _bitMap = new BitmapImage();
              _bitMap.SetSource(e.Result);
              im = stkpnlImage.FindName("img" + (i + 1).ToString()) as Image;
              tb = this.stkpnlImage.FindName("tb" + (i + 1).ToString()) as TextBlock;
              tb.Text = ((i + 1).ToString()) + "/" + p.Count;
              im.Source = _bitMap;
              im.Source = null;
              i++;
              LoadImage();//关键,继续加载下一个(是不是有点ajax中http长连接的意思,呵)
            }
          
          
        }
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
          LoadImage();
        }
        int i=0;
        void LoadImage()
        {
          if (i < p.Count)
          {
            if (p != null && p.Count > 0)
            {
              string[] url = p[i].imageurl.Split('/');
              string urls = url[1] + "/" + url[2] + "/" + url[3];
              _wc.OpenReadAsync(new Uri(urls, UriKind.Relative));
            }
          }
    
        }
    
    
      }
    }

    我這邊有一個問題要請教一下,就是為什麼我用WEBCLIENT 加載 600張圖片 循環執行到540就出現了內存不足的情況要怎麼釋放

    2011年8月23日 3:28

答案

  • GC就可以回收内存!不过你需要有这么多图片显示在界面上吗?

    如果不是这样的话,你可以不要这么弄。只把显示的图片加载下来。

    如果你一次就需要显示这么多,最好是前台用writeablemap拼一个。


    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2011年8月24日 12:58
    版主