Is CoreDispatcher.RunIdleAsync the correct method call to replace the apartment_context, resume_background combination in the main_async function below? If this was correct, would it then follow that the five lines of code from "let stream..." to "let result..." should then be moved into the body of the RunIdleAsync?
#![windows_subsystem = "windows"]
#![allow(non_snake_case)]
use windows::{
core::*,
ApplicationModel::Activation::LaunchActivatedEventArgs,
Win32::System::Com::*,
UI::{
Colors,
Xaml::{
Controls::{
TextBlock,
},
Application,
ApplicationInitializationCallback,
Window,
Media::{
FontFamily,
SolidColorBrush,
},
TextAlignment,
TextWrapping,
VerticalAlignment,
FrameworkElement,
IApplicationOverrides,
IApplicationOverrides_Impl,
},
},
Storage::{
Pickers::*,
FileAccessMode,
StorageFile,
},
Graphics::Imaging::BitmapDecoder,
Media::Ocr::*,
};
#[implement(IApplicationOverrides)]
struct App();
impl IApplicationOverrides_Impl for App {
fn OnLaunched(&self, _: &Option<LaunchActivatedEventArgs>) -> Result<()> {
let block = TextBlock::new()?;
block.SetFontFamily(FontFamily::CreateInstanceWithName(HSTRING::from("Segoe UI Semibold"))?)?;
block.SetFontSize(72.0)?;
block.SetForeground(SolidColorBrush::CreateInstanceWithColor(Colors::Orange()?)?)?;
let fe = FrameworkElement::from(&block);
fe.SetVerticalAlignment(VerticalAlignment::Center)?;
block.SetTextAlignment(TextAlignment::Center)?;
block.SetTextWrapping(TextWrapping::Wrap)?;
let window = Window::Current()?;
window.SetContent(&block)?;
window.Activate()?;
futures::executor::block_on(main_async(block, window))
}
}
fn main() -> Result<()> {
unsafe {
CoInitializeEx(std::ptr::null(), COINIT_MULTITHREADED)?;
}
Application::Start(ApplicationInitializationCallback::new(|_| {
Application::compose(App())?;
Ok(())
}))
}
async fn main_async(block: TextBlock, window: Window) -> Result<()> {
let picker = FileOpenPicker::new()?;
picker.FileTypeFilter()?.Append(HSTRING::from(".png"))?;
picker.SetSuggestedStartLocation(PickerLocationId::PicturesLibrary)?;
let file: StorageFile = picker.PickSingleFileAsync()?.await?;
//apartment_context ui_thread;
//co_await resume_background();
let stream = file.OpenAsync(FileAccessMode::Read)?.await?;
let decoder = BitmapDecoder::CreateAsync(stream)?.await?;
let bitmap = decoder.GetSoftwareBitmapAsync()?.await?;
let engine = OcrEngine::TryCreateFromUserProfileLanguages()?;
let result = engine.RecognizeAsync(bitmap)?.await?;
//co_await ui_thread
block.SetText(result.Text()?)?;
Ok(())
}
With further revision: