私はコードを学習し、google streetview screenshostをキャプチャするアプリをやっています。多くのスクリーンショット。私は、クエリを並列化する方法を見つけたいと思っています。c#uwp:ディスパッチwebviewナビゲーションと各avaible論理CPUのキャプチャ
ここで一度に単一のクエリのためのコード:
// prepare the html code string in a dictionary (I use street view in a iframe)
Dictionary<int, string> htmlCode= SetUpURLListHD();
// prepare the webview and display it on a grid
WebView saveWebView = new WebView();
saveWebView.Width = Width;
saveWebView.Height = Height;
TopGrid.Children.Add(saveWebView);
// navigate to string, wait a moment until street view completely loaded, do a capture, save it to file m.jpg with custom function
for (var m = 0; m < htmlCode.Count; m++)
{
saveWebView.NavigateToString(htmlCode[m]);
await Task.Delay(2000);
await saveWebView.CapturePreviewToStreamAsync(stream);
await SaveSSAsync(stream, m);
}
私は16論理CPU、RAMの64Go、およびファイバ接続を持っています。だから、私は時間に16のクエリまでやりたいそのために
、私は私が16 webviews作成するために、16個の機能を記述する必要があると思う:
async Task ProcessURLHD1Async(string url, int i, int width, int height, ulong firstFrameSize)
{
try
{
WebView saveWebView1 = new WebView();
saveWebView1.Width = width;
saveWebView1.Height = height;
TopGrid.Children.Add(saveWebView1);
...
セカンド機能:
async Task ProcessURLHD2Async(string url, int i, int width, int height, ulong firstFrameSize)
{
try
{
WebView saveWebView2 = new WebView();
saveWebView2.Width = width;
saveWebView2.Height = height;
TopGrid.Children.Add(saveWebView2);
など...
私は知りませんもしそれが正しい方法であれば。 私は今16の関数を同時に実行しようとしています。終了すると、次のhtmlコード(辞書にコード化された次のストリートビューの画像)で再び開始します。 今のところ、結果はありません。 何か助けを歓迎します。
ありがとうございました!
ps:このサイトで助けてくれてありがとう、何週間ものコーディングの後、私の最初のメッセージです。
編集:OK!私は見つけた!それは醜いですが、速いです! :
// below namespace :
public static class DispatcherTaskExtensions
{
public static async Task<T> RunTaskAsync<T>(this CoreDispatcher dispatcher,
Func<Task<T>> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
var taskCompletionSource = new TaskCompletionSource<T>();
await dispatcher.RunAsync(priority, async() =>
{
try
{
taskCompletionSource.SetResult(await func());
}
catch (Exception ex)
{
taskCompletionSource.SetException(ex);
}
});
return await taskCompletionSource.Task;
}
// There is no TaskCompletionSource<void> so we use a bool that we throw away.
public static async Task RunTaskAsync(this CoreDispatcher dispatcher,
Func<Task> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) =>
await RunTaskAsync(dispatcher, async() => { await func(); return false; }, priority);
}
//Call function on click
private async void GetHDMT_Click(object sender, RoutedEventArgs e)
{
if (outputFolder == null)
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
outputFolder = await folderPicker.PickSingleFolderAsync();
if (outputFolder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.
FutureAccessList.AddOrReplace("PickedFolderToken", outputFolder);
}
else
{
}
}
if (mylat.Count > 0)
{
await GetImagesAsyncHD();
}
}
private async Task GetImagesAsyncHD()
{
// Make a list of html code with street view iframe.
var Width = 4000;
var Height = 2000;
var urls = SetUpURLListHD(Width, Height);
WebView saveWebView0 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView0);
WebView saveWebView1 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView1);
WebView saveWebView2 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView2);
WebView saveWebView3 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView3); WebView
saveWebView4 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView4);
WebView saveWebView5 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView5);
WebView saveWebView6 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView6);
WebView saveWebView7 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView7); WebView
saveWebView8 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView8);
WebView saveWebView9 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView9);
WebView saveWebView10 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView10);
WebView saveWebView11 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView11);
WebView saveWebView12 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView12);
WebView saveWebView13 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView13);
WebView saveWebView14 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView14);
WebView saveWebView15 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView15);
List<Task> tasks = new List<Task>();
Task t0 = Task.Run(async() =>
{
for (var j=0; j<urls.Count; j=j+16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t0);
Task t1 = Task.Run(async() =>
{
for (var j = 1; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t1);
Task t2 = Task.Run(async() =>
{
for (var j = 2; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t2);
Task t3 = Task.Run(async() =>
{
for (var j = 3; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t3);
Task t4 = Task.Run(async() =>
{
for (var j = 4; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t4);
Task t5 = Task.Run(async() =>
{
for (var j = 5; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t5);
Task t6 = Task.Run(async() =>
{
for (var j = 6; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t6);
Task t7 = Task.Run(async() =>
{
for (var j = 7; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t7);
Task t8 = Task.Run(async() =>
{
for (var j = 8; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t8);
Task t9 = Task.Run(async() =>
{
for (var j = 9; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t9);
Task t10 = Task.Run(async() =>
{
for (var j = 10; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t10);
Task t11 = Task.Run(async() =>
{
for (var j = 11; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t11);
Task t12 = Task.Run(async() =>
{
for (var j = 12; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t12);
Task t13 = Task.Run(async() =>
{
for (var j = 13; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t13);
Task t14 = Task.Run(async() =>
{
for (var j = 14; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t14);
Task t15 = Task.Run(async() =>
{
for (var j = 15; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t15);
await Task.WhenAll(tasks);
}
public static async Task ProcessURLHD0Async(string url, int i, int width, int height, ulong firstFrameSize, WebView saveWeb, StorageFolder folder)
{
try
{
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
saveWeb.NavigateToString(url);
});
await Task.Delay(3000);
var kk = 0;
var pixelised = true;
while (pixelised == true)
{
ulong thissize = await CaptureAndSave(saveWeb, i, folder);
if ((thissize > 0.7 * firstFrameSize) || kk > 5)
{
pixelised = false;
}
else
{
await Task.Delay(1000);
kk = kk + 1;
}
}
}
catch (Exception)
{
throw;
}
}
public static async Task<ulong> CaptureAndSave(WebView webv, int i, StorageFolder folder)
{
ulong size = 0;
SoftwareBitmap softwareBitmap = null;
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
BitmapDecoder decoder = null;
// Changes to imageElement must happen on the UI thread.
await webv.Dispatcher.RunTaskAsync(async() =>
{
await webv.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
});
StorageFile file_Save = await folder.CreateFileAsync(i + ".jpg", CreationCollisionOption.ReplaceExisting);
if (file_Save != null)
{
using (var streamF = await file_Save.OpenAsync(FileAccessMode.ReadWrite))
{
//StorageFile file_Save = await outputFolder.CreateFileAsync(i + "_" + equals + "_" + size + ".jpg", CreationCollisionOption.ReplaceExisting);
//< encoder to save >
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, streamF);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
//</ encoder to save >
//-</ Save Bitmap as File >-
}
}
var basicProperties = await file_Save.GetBasicPropertiesAsync();
size = basicProperties.Size;
while (size < 21)
{
await Task.Delay(100);
basicProperties = await file_Save.GetBasicPropertiesAsync();
size = basicProperties.Size;
}
stream.Dispose();
softwareBitmap.Dispose();
return size;
}
結果:パラレル化なしで278に対して20秒で118キャプチャ(合計= 139Mo)!最大5GBのRAMを使用します。
編集2: 絶食する...上記のコードに誤りがあった私は(Savewebview1、Savewebview2 ....)を補正するとき、私はいつも、Savewebview0を使用し、結果されています... 400秒... すべての作業はメインスレッドで行いますが、私は他のスレッドでWebviewを作成する必要がありますが、それは不可能だと思います... 誰でもアイデアがあれば... :(