WinRT/C++ co_await resume_foreground(Dispatcher()) cause try_as runtime error - c++-winrt

What causes the try_as() runtime error to occur when calling co_await resume_foreground(Dispatcher()) in the LoadOverallStatisticsAsync() function.
Note: using resume_foreground(Dispatcher()) is only for the purpose of ensuring that the function runs on the UI Thread.
HomePage.cpp
fire_and_forget HomePage::HomeLoaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
loadAction = [lifetime = get_strong()]() -> winrt::Windows::Foundation::IAsyncAction
{
...
co_await lifetime->LoadOverallStatisticsAsync();
...
}();
co_await loadAction;
}
winrt::Windows::Foundation::IAsyncAction HomePage::LoadOverallStatisticsAsync()
{
co_await resume_foreground(Dispatcher()); // try_as() runtime error occurs here
apartment_context ui_thread;
co_await resume_background();
// DoThings();
co_await ui_thread;
// Update UI
}
Edit: this code is run on a UWP project in a WinRT/C++ Desktop environment

Related

ObjectDisposedException on scrollview renderer

I am firing the following event in my Xamarin application:
AppEvents.Instance.UI.RiseSearchStringTypingEvent(this, new EventArgs());
The HomePage.xaml.cs codebehind subscriber-event performs an auto scroll to top every time the event is being fired (on every input on the search field)
void OnSearchStringTyping(object sender, EventArgs e)
{
scrollView.ScrollToAsync(0.0, 0.0, true);
}
when the searchstring is complete and the user hits the submit-button, the Oncompleted() method invokes on the SearchHeaderView.xaml.cs which navigates to the SearchPage
void OnCompleted(object sender, EventArgs e)
{
var tag = this + ".OnCompleted";
try
{
if (string.Compare(LastSearchText, SearchEntry.Text) == 0) return
if (Device.RuntimePlatform == Device.Android)
{
SearchRequest();
}
else if (Device.RuntimePlatform == Device.iOS)
{
var task = new Task(SearchRequest);
task.Start();
}
LastSearchText = SearchEntry.Text;
}
catch (Exception ex)
{
Track.Exception(tag, ex);
}
}
This procedure works perfect on iOS platforms...But in android, when i submit my search string, the app crashes with an ObjectDisposedException on the ScrollView
I think the problem is that the application tries to scroll up the view again after it navigates away from the homepage. The SearchRequest() method on Android is not invoked in a new Thread, but iOS is. How can i fix this? I can not just make the method being invoked asynchronously, because the Search doesn't work then.

Plugin Development: Eclipse hangs when testing plugin

I am new to developing plugins, and was wondering what causes a test plugin to hang when started i.e. Eclipse is unresponsive.
I know that my code is working as I developed a voice recognition plugin to write to the screen what is said and when I open notepad everything I say is printed to notepad.
So I was wondering, am I missing something in the plugin life-cycle that causes the IDE to hang when my plugin is started?
package recognise.handlers;
public class SampleHandler extends AbstractHandler {
public SampleHandler() {
}
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
boolean finish = false;
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(),
"Recognise",
"Starting Recognition");
TakeInput start = new TakeInput();
//Stage a = new Stage();
//SceneManager scene = new SceneManager();
try {
start.startVoiceRecognition(finish);
//scene.start(a);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
return null;
}
}
Does the start.startVoiceRecognition() need to be threaded?
Thanks in advance and let me know if you would like to see my manifest/activator etc.
Conclusion
Added a job separate to the UI thread
/*
* Start a new job separate to the main thread so the UI will not
* become unresponsive when the plugin has started
*/
public void runVoiceRecognitionJob() {
Job job = new Job("Voice Recognition Job") {
#Override
protected IStatus run(IProgressMonitor monitor) {
TakeInput start = new TakeInput();
try {
start.startVoiceRecognition(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// use this to open a Shell in the UI thread
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
As shown start.startVoiceRecognition() is running in the UI thread, and it will block the UI thread until it is finished and the app will be unresponsive during that time. So if it is doing a significant amount of work either use a Thread or use an Eclipse Job (which runs work in a background thread managed by Eclipse).
To unblock your UI you have to use Display thread.
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
boolean finish = false;
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(),
"Recognise",
"Starting Recognition");
TakeInput start = new TakeInput();
//Stage a = new Stage();
//SceneManager scene = new SceneManager();
try {
start.startVoiceRecognition(finish);
//scene.start(a);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
MessageDialog.openInformation(shell, "Your Popup ",
"Your job has finished.");
}
});
return null;
}
You can use Display.getDefault().asyncExec() as mentioned above, so your UI will be unblocked, while your non UI code will be executing.

How to "catch" unhandled Exceptions

We've developed a .NET 3.5 CF Application and we're experiencing some application crashes due to unhandled exceptions, thrown in some lib code.
The application terminates and the standard application popup exception message box is shown.
Is there a way to catch all unhandled exceptions? Or at least, catch the text from the message box. Most of our customers simply restart the device, so that we're not able to have a look on the exception message box.
Any ideas?
Have you added an UnhandledException event handler?
[MTAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
// start your app logic, etc
...
}
static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
// do something with the info here - log to a file or whatever
MessageBox.Show(exception.Message);
}
I do something similar to what ctacke does.
private static Form1 objForm;
[MTAThread]
static void Main(string[] args)
{
objForm = new Form1();
try
{
Application.Run(objForm);
} catch (Exception err) {
// do something with the info here - log to a file or whatever
MessageBox.Show(err.Message);
if ((objForm != null) && !objForm.IsDisposed)
{
// do some clean-up of your code
// (i.e. enable MS_SIPBUTTON) before application exits.
}
}
}
Perhaps he can comment on whether my technique is good or bad.

Error in Windows 8 xaml Modern app with thread

I have following two methods. When user clicks on start button from ui, the step geoLocator_PositionChanged in geoLocator_PositionChanged method is fired and calls the other method geoLocator_PositionChanged.But when it comes to try block while executing the first statement it throws the following error:
"The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"
private async void btnStartStop_Click_1(object sender, RoutedEventArgs e)
{
geoLocator.PositionChanged += geoLocator_PositionChanged;
}
async void geoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
MessageDialog msgdlg = null;
bool bDisplayDialog = false;
try
{
lblAltValue.Text = args.Position.Coordinate.Altitude.ToString();
}
catch
{
}
}
Any help how can I fix this issue ?
You try to access the UI-Thread from another one.
Try something like ths
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
//HERE GOES THE UI ACCESS LIKE this.textbox.text = "MY AWESOME TEXT";
});

Handle error at non-UI thread

For some reason unhandled exception which occur at non-UI thread don't handled by App_UnhandledException handler.
This approach works well for Windows Phone apps to globally handle, track and analyze exceptions but doesn't work for Windows 8 apps.
this.UnhandledException += App_UnhandledException; //doesn't handle
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var task = new Task(() => { throw new NullReferenceException("Test exc in UI thread"); });
task.Start();
}
Please, advise.
Using the UnobservedTaskException event of TaskScheduler you can catch all exceptions in Tasks that are not awaited. Just to clarify: If you await tasks exceptions are propagated to the UI thread and can hence be catched via Application.UnhandledException.
Using the new async/await keywords:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
var task = new Task(() => { throw new NullReferenceException("Test exc in UI thread"); });
task.Start();
try
{
await task;
}
catch (Exception ex)
{
var msg = new MessageDialog(ex.ToString(), "An error has occurred");
await msg.ShowAsync();
}
}
Using just the Task methods:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var task = new Task(() => { throw new NullReferenceException("Test exc in UI thread"); });
task.ContinueWith(t =>
{
var msg = new MessageDialog(t.Exception.ToString(), "An error has occurred");
msg.ShowAsync().Start();
}, TaskContinuationOptions.OnlyOnFaulted);
task.Start();
}
For catching all unhandled exceptions, see this question:
How do I add a Global Exception handler to a Metro Style App?