处置在C#中的循环中创建的对象 [英] Disposing The Objects created in a loop in C#

查看:346
本文介绍了处置在C#中的循环中创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经提取了视频的总帧数",并保存在输出目录"中.
现在,我已经在WPF窗口的列表框中显示了所有服务器场以及框架号.在每个框架上.

Hi all,

I have extracted the Total frames of a video and saved in a Output Directory.
Now i have displayed all the farmes in a listbox on WPF window along with the frame no. on each frame.

int FramesCount = 0;
List<stackpanel> splist = new List<stackpanel>();				
System.IO.DirectoryInfo myimagesdir = new System.IO.DirectoryInfo(@"E:\\tmp");
foreach(System.IO.FileInfo myimagesfile in myimagesdir.GetFiles("*.jpg"))
{
    FramesCount = FramesCount + 1;
    StackPanel sp = new StackPanel();
    sp.Height = 100; sp.Width = 125;			
									
    Image img = new Image();
    img.Height = 80; img.Width = 100;
    BitmapImage bitimg = new BitmapImage();
					
    bitimg.BeginInit();
    bitimg.UriSource = new Uri(myimagesfile.FullName);
    bitimg.CacheOption = BitmapCacheOption.OnLoad;
					
    bitimg.EndInit();
    img.Source = bitimg;
				
    TextBlock tb = new TextBlock();
    tb.Height = 20; tb.Width = 30; tb.FontSize = 15;
    tb.Text = FramesCount.ToString();
    sp.Children.Add(tb);					
    sp.Children.Add(img);
    splist.Add(sp);								
}
			
Frameslistbox.ItemsSource = splist;



在这里,我为每个文件创建一个堆栈面板,图像和文本块.图像和文本块作为子代添加到堆栈面板,并且此堆栈面板被添加到列表中.

现在,每次创建新对象时都在这里.如何在foreach循环中放置旧对象?

感谢



Here for each file I am creating a stackpanel, image and textblock. image and textblock are added as children to stackpanel and this stackpanels are added to a list.

Now here every time new objects are created. How can i dispose the old objects in foreach loop?

Thanks

推荐答案

对于处置对象,我们可以调用

For disposing object we can call the Dispose method or wrap the object with a using block.

在您的情况下,您创建BitmapImageImageTextBlockStackPanel.这些类均未实现 IDisposable .因此,您不需要处理这些对象,只需将它们从列表中删除(然后没人会对其进行引用),然后

In your case, you create BitmapImage, Image, TextBlock and StackPanel. None of these classes implement IDisposable. So, you don''t need to dispose the objects, just remove them from the list (and then no one will have a reference to them), and the GC will free them.


就像Shmuel Zang所说的那样,您无需在意在给定的上下文中.您正在创建一些对象并将它们放在列表中.
在完成for循环的当前迭代之后,它们将保留对对象的唯一引用.而且由于该列表应该包含对这些对象的引用,因此处置它们不是一个好主意.
如果过了一段时间,列表将其引用丢弃了,则垃圾收集器将处理未引用的对象.

例如:您不能创建new Image();,因为Image是抽象类.相反,您可以直接
Image img = new Bitmap(myimagesfile.FullName);.
Just as Shmuel Zang said, you don''t need to care about that in the given context. You are creating some objects and put them in a list.
After the current iteration of the for loop is done, the holds the only references to your objects. And since the list is supposed to hold references to the objects, disposing them would not be a good idea.
If, some time later, the list throws its references away, the Garbage Collector will take care of the unreferenced objects.

As for the example: you can''t create a new Image(); because Image is an abstract class. Instead you can directly
Image img = new Bitmap(myimagesfile.FullName);.


在添加新对象之前,请先遍历spList子级,然后逐一处理它们,
然后将新的子代添加到spList.
Before adding new objects iterate through spList children and dispose them one by one ,
and then add new children to spList.


这篇关于处置在C#中的循环中创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆