Taglib性能问题



我想更快地读取批量音频文件的标记。目前,我能够在大约7秒内将5000个音频文件信息存储到结构列表中。

问题是,当我选择有20000或40000个文件的文件夹时,应用程序会继续运行,并且不会通知进程已经完成。而当它读取5000个文件时,它显示消息框;完成加载文件5000〃;在7秒内。

这是我的代码:

public struct SongInfoStruct
{
public string Key;
public string Value;
public string Artist;
public double Duration;
public string Comments;
public string Album;
public string Missing;
};
public async Task<SongInfoStruct> GetSongInfo(string url)
{
var songinfo = (dynamic)null;
var tagFile = TagLib.File.Create(url);
var songName = (dynamic)null;
var artist = (dynamic)null;
var album = (dynamic)null;
var comments = (dynamic)null;
var duration = (dynamic)null;
await Task.Run(() =>
{
songName = tagFile.Tag.Title;
artist = tagFile.Tag.FirstPerformer;
album = tagFile.Tag.Album;
comments = tagFile.Tag.Comment;
duration = tagFile.Properties.Duration.TotalSeconds;
});
return songinfo = new SongInfoStruct
{
Key = url,
Value = songName,
Artist = artist,
Duration = duration,
Comments = comments,
Album = album,
Missing = " "
};
}
public async Task<List<SongInfoStruct>> ReadPathFromSource(string Source)
{
var files = Directory.EnumerateFiles(Source, "*", 
SearchOption.AllDirectories).Where(s => s.EndsWith(".mp3") || 
s.EndsWith(".m4a"));
int length = files.Count();
var listpaths = new List<SongInfoStruct>(length);
listpaths.Clear();
foreach (string PathsClickSong_temp in files)
{
var item = await GetSongInfo(PathsClickSong_temp);
await Task.Run(() =>
{
listpaths.Add(item);
});
}
MessageBox.Show("Done loading files "+ listpaths.Count.ToString());
return listpaths;
}

作为一种好的做法,除非您想使用相同的调用方上下文,否则请始终尝试将ConfigureAwait设置为false

var item = await GetSongInfo(PathsClickSong_temp).ConfigureAwait(false);

在这里,您可以使用Task.WhenAll来尝试它,它可以同时获取多个数据。Task.Run在有CPU密集工作时很好使用,详细信息请参阅何时正确使用Task.Run和何时异步等待

var listpaths = new List<Task<SongInfoStruct>>(length);
listpaths.Clear();

foreach (string PathsClickSong_temp in files)
{
var item = GetSongInfo(PathsClickSong_temp);
listpaths.Add(item);

}
await Task.WhenAll(listpaths);

最新更新