I have read up about this problem I am having and I cannot seem to find a solution for it. Some people have recommended to use the dispatcher to push the code execution onto the UI Thread. However this does not solve my issue. I am calling this code out of an Asyc method which is before hand called by a Guide object to display a notification. The code which controls the UI stuff is as follows:
public bool isDisabled
{
set
{
if (value)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
color = new SolidColorBrush(Colors.Gray);
});
enabled = false;
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
color = origionalColor;
});
enabled = true;
}
}
}
and it is being called from
private void nameInputFinished(IAsyncResult ar)
{
int? result = Guide.EndShowMessageBox(ar);
if (result == 0)
{
[...]
JourneyPanels.getPanel(JourneyPanelTypes.integration).isDisabled = false;
}
}
Not sure why I am getting the corss-thread exception but I am pretty sure it has something to do with the async method nesting.
Any help is greatly appreciated!
EDIT: Full Exception Stack
{System.UnauthorizedAccessException: Invalid cross-thread access.
at MS.Internal.XcpImports.CheckThread()
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, Boolean b)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, Boolean b)
at System.Windows.Controls.TextBox.set_IsReadOnly(Boolean value)
at PocketRitual.Journey.nameInputFinished(IAsyncResult ar)
at Microsoft.Xna.Framework.GamerServices.ActionDialogHelper.Complete()
at Microsoft.Xna.Framework.GamerServices.ActionDialogHelper.GetMessageBoxResult(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}
It’s your calling code you need to run on the main UI thread e.g.
There is no need to call
Dispatcheron your internal property changes because you will be running on the UI thread at that point.