Jump to content
Sign in to follow this  
Guest andreif

Can't execute sync version of API from background thread

Recommended Posts

Guest andreif

(Silverlight version) I’m trying to do it like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(x => Refresh());
    }

    void Refresh()
    {
        try
        {
            var client = new Client(RPC_URI);
            client.LogIn(USERNAME, PASSWORD);
            var news = client.ListNewsHeadlines("UK", 10);
            client.LogOut();
        }
        catch (Exception exc)
        {
        }
    }

And getting this exception:

System.UnauthorizedAccessException: Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at MS.Internal.XcpImports.ApplicationGetVisualRoot() at System.Windows.Application.getRootVisual() at CityIndex.JsonClient.Client.RequestTDTO at CIAPI.Rpc.Client.LogIn(String userName, String password) at CityIndexNewsWidget.MainPage.Refresh()

Share this post


Link to post
Guest sky.sanders

I have confirmed this as a bug in the synchronous methods in the silverlight client.

For the time being, please utilize an async pattern by using the begin/end methods.

Share this post


Link to post
Guest mrdavidlaing

 

(Silverlight version) I’m trying to do it like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(x => Refresh());
    }

    void Refresh()
    {
        try
        {
            var client = new Client(RPC_URI);
            client.LogIn(USERNAME, PASSWORD);
            var news = client.ListNewsHeadlines("UK", 10);
            client.LogOut();
        }
        catch (Exception exc)
        {
        }
    }

And getting this exception:

System.UnauthorizedAccessException: Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at MS.Internal.XcpImports.ApplicationGetVisualRoot() at System.Windows.Application.getRootVisual() at CityIndex.JsonClient.Client.RequestTDTO at CIAPI.Rpc.Client.LogIn(String userName, String password) at CityIndexNewsWidget.MainPage.Refresh()

Could you point me at the relevant GitHub commit so I can reproduce this locally.

Share this post


Link to post
Guest ryanholder

 

(Silverlight version) I’m trying to do it like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(x => Refresh());
    }

    void Refresh()
    {
        try
        {
            var client = new Client(RPC_URI);
            client.LogIn(USERNAME, PASSWORD);
            var news = client.ListNewsHeadlines("UK", 10);
            client.LogOut();
        }
        catch (Exception exc)
        {
        }
    }

And getting this exception:

System.UnauthorizedAccessException: Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at MS.Internal.XcpImports.ApplicationGetVisualRoot() at System.Windows.Application.getRootVisual() at CityIndex.JsonClient.Client.RequestTDTO at CIAPI.Rpc.Client.LogIn(String userName, String password) at CityIndexNewsWidget.MainPage.Refresh()

@mrdavidlaing just adding a link here to Andrei’s git repo from the link he posted on the Buzz site:https://github.com/fandrei/CityIndexNewsWidget

Share this post


Link to post
Guest andreif

 

(Silverlight version) I’m trying to do it like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(x => Refresh());
    }

    void Refresh()
    {
        try
        {
            var client = new Client(RPC_URI);
            client.LogIn(USERNAME, PASSWORD);
            var news = client.ListNewsHeadlines("UK", 10);
            client.LogOut();
        }
        catch (Exception exc)
        {
        }
    }

And getting this exception:

System.UnauthorizedAccessException: Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at MS.Internal.XcpImports.ApplicationGetVisualRoot() at System.Windows.Application.getRootVisual() at CityIndex.JsonClient.Client.RequestTDTO at CIAPI.Rpc.Client.LogIn(String userName, String password) at CityIndexNewsWidget.MainPage.Refresh()

@mrdavidlaing I’ve pushed it, get the last version

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×