Async operations...

2 posts, 0 answers
  1. Morgan
    Morgan avatar
    81 posts
    Member since:
    Dec 2009

    Posted 30 Nov 2013 Link to this post

    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-



  2. Ventzi
    Admin
    Ventzi avatar
    114 posts

    Posted 04 Dec 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top