我正在嘗試更快地閱讀我的 zip 以使我的 foreach 回圈并行。
但是我怎么能根據這種方法做到這一點呢?
private async Task GetImage()
{
try
{
PathToZip();
int currentIndex = GetCurrentIndex();
Content content = _protocol.contents[currentIndex];
string imageName = $"{content.contentid}.jpg";
using (ZipArchive archive = ZipFile.OpenRead(PathToZip()))
{
foreach (ZipArchiveEntry pictureEntry in archive.Entries)
{
if (string.Equals(pictureEntry.Name, imageName, StringComparison.OrdinalIgnoreCase))
{
//for reading the image
byte[] buffer;
long length = pictureEntry.Length;
buffer = new byte[length];
pictureEntry.Open().Read(buffer, 0, (int)length);
myImage.Source = ImageSource.FromStream(() => new MemoryStream(buffer));
}
}
}
}
catch (Exception)
{
}
}
我怎樣才能做到這一點?
提前致謝!
uj5u.com熱心網友回復:
我不確定您在尋找什么,但這里是您可以根據您的查詢完成它的方法。請記住,您在 foreach 塊中的邏輯必須是“并行就緒的”。
Paraller.foreach (archive.Entries,(pictureEntry)=>
{
if (string.Equals(pictureEntry.Name, imageName, StringComparison.OrdinalIgnoreCase))
{
//for reading the image
byte[] buffer;
long length = pictureEntry.Length;
buffer = new byte[length];
pictureEntry.Open().Read(buffer, 0, (int)length);
myImage.Source = ImageSource.FromStream(() => new MemoryStream(buffer));
}
});
您也可以參考官方檔案 - [1]:https ://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop?redirectedfrom =MSDN
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442900.html
標籤:C# 多线程 并行处理 并行.foreach
