This is a migrated thread and some comments may be shown as answers.

Async operations...

1 Answer 543 Views
Window
This is a migrated thread and some comments may be shown as answers.
Morgan
Top achievements
Rank 1
Iron
Morgan asked on 30 Nov 2013, 07:56 PM

RadWindow.Alert will not play nice with async operations as follows...


await _datacontext.SaveChangesAsync().ContinueWith((args) => {

   RadWindow.Alert("Saved!", "Success!");

}).ConfigureWait(false);



or



await _datacontext.SaveChangesAsync().ConfigureWait(false);

RadWindow.Alert("Saved!", "Success!");



throws this...



An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in WindowsBase.dll

Additional information: Exception has been thrown by the target of an invocation.


Exception:Thrown: "The calling thread must be STA, because many UI components require this." (System.InvalidOperationException)

A System.InvalidOperationException was thrown: "The calling thread must be STA, because many UI components require this."

Time: 11/30/2013 13:51:44

Thread:Worker Thread[6864]



I thought it was just me and Caliburn.Micro but its truly the control...



-Windows 8.1, Desktop Application, Lastest controls, Visual Studio 2013, Entity Framework 6.0-



1 Answer, 1 is accepted

Sort by
0
Ventzi
Telerik team
answered on 04 Dec 2013, 02:47 PM
Hi Morgan,

You are trying to create an instance of the RadWindow on different thread which cause a cross threading issue. WPF is based heavily on DependencyObjects, which can only be hosted in a STA thread. Creating them in a non-STA thread results in exceptions. So you need to start a task which is running on the STA thread. The code could look something like the following:

private async void OpenWindow()
{
    await SaveChangesAsync();
   await Task.Factory.StartNew
   (
     () =>
       {
          RadWindow.Alert("Saved!");
       },
       CancellationToken.None,
       TaskCreationOptions.None,
       TaskScheduler.FromCurrentSynchronizationContext()
   );
}

 Another possible solution is to use Dispatcher.Invoke/BeginInvoke methods.

Hope this helps.

Regards,
Ventzi
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
Window
Asked by
Morgan
Top achievements
Rank 1
Iron
Answers by
Ventzi
Telerik team
Share this question
or