简体   繁体   English

C#中的循环优化

[英]loop optimization in c#

I am working on a music player for pc using c# and all seems to go fine but, i have problem in loading all the music files from the music directory because it loads very slowly and this take time when opening the app, it could take 5 min depending on the amount of music files.我正在使用 c# 为 pc 开发音乐播放器,一切似乎都很好,但是,我在从音乐目录加载所有音乐文件时遇到问题,因为它加载速度非常慢,这在打开应用程序时需要时间,可能需要 5 min 取决于音乐文件的数量。 I think this happens because i created a loop to loop through each music file and get the metadatas and also the picture to load on different picture boxes for each music file.我认为发生这种情况是因为我创建了一个循环来循环遍历每个音乐文件并获取元数据以及图片以加载到每个音乐文件的不同图片框上。
Please Help, i need it to be faster.请帮助,我需要它更快。 Thank you.谢谢你。 the code is below...代码在下面...

    public List<MusicDetails> Music_Library()
    {


         List<MusicDetails> files = new List<MusicDetails>();
        string[] musicfolder = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),"*mp3", SearchOption.AllDirectories);


        for (int i = 0; i <musicfolder.Length; i++)
        {
            try {
                files.Add(new MusicDetails
                {
                    title = TagLib.File.Create(musicfolder[i]).Tag.Title,
                    genre = TagLib.File.Create(musicfolder[i]).Tag.FirstGenre,
                    artist = TagLib.File.Create(musicfolder[i]).Tag.FirstPerformer,
                    path = musicfolder[i],
                    CoverArt = OrganiseAlbums.SingleAlbumImage(musicfolder[i],true)


                });
            }catch(Exception)
            {
              // OMIT FILE
            }  
        }
        return files;

    }

You could try replacing your loop with a parallel foreach loop using background threads - add each item to the UI as it is processed, and let .Net determine the most efficient way to process everything.您可以尝试使用后台线程将循环替换为并行 foreach 循环 - 在处理时将每个项目添加到 UI,并让 .Net 确定处理所有内容的最有效方式。 If you do it right, your UI will remain responsive, and your users will start out with "enough to look at..." Here is a link to get you started:如果你做对了,你的 UI 将保持响应,你的用户将开始“足够看......”这是一个让你开始的链接:

https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

If you have a ton of music files, though, you may not want to load them all into memory at once.但是,如果您有大量音乐文件,您可能不想一次将它们全部加载到内存中。 I would look into some sort of a list control or layout control that allows virtualization so that you instantiate and display only the visible ones at any given time.我会研究某种允许虚拟化的列表控件或布局控件,以便您在任何给定时间仅实例化和显示可见的控件。 Use a tutorial to see how to bind to your collection so that it is virtualized.使用教程了解如何绑定到您的集合以使其虚拟化。

If you post a more specific question with examples of what you have tried, you will get more specific answers...如果您发布一个更具体的问题以及您尝试过的示例,您将获得更具体的答案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM