Hello,
Welcome to our Microsoft Q&A platform!
If you intend to access UI resources in a non-UI thread, use Dispatcher.RunAsync
:
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Task.Run(async () => {
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var bmp = await BitmapFactory.FromStream(stream);
// other code
}
});
});
You mentioned that you need to save multiple pictures so that you can use Task for multi-tasking parallel processing.
var tasks = new List();
tasks.Add(Task.Run(async () =>
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var bmp = await BitmapFactory.FromStream(stream);
// other code
}
});
}));
await Task.WhenAll(tasks.ToArray());
Task.WhenAll
will assign tasks according to the current CPU's computing power, and can handle multiple tasks at the same time, which will greatly speed up your image task processing speed.
In this way, you can achieve your goals.
Thanks.