Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AggregateException when invoke UI Thread in windows Phone 8.1. Crashes only from windows store.

Discussion in 'Windows' started by stamenkrumov, Mar 18, 2015.

  1. stamenkrumov

    stamenkrumov

    Joined:
    Jun 15, 2014
    Posts:
    33
    Hello,
    I have a strange problem with my game for windows phone 8.1. I've integrated the vserv sdk for windows 8.1. I implemented the ads caching and showing in the App.xaml.cs in Visual Studio because it was the only way to get them working. First I tried to call the VServ's cache ad method directly but I got the following exception:

    Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
    at Windows.UI.Xaml.Controls.ProgressRing..ctor()
    at com.vserv.windows.ads.wp81.VservAdView.InstantiateProgressRing()
    at com.vserv.windows.ads.wp81.VservAdView.CacheAd()
    at Template.App.SendShowAdMessage_showAdEvent()
    at SendShowAdMessage.sendShowAdMessage()
    at PresentAnimation.OnPresentPressed()
    at PresentAnimation.$Invoke4(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)

    Then I changed the code like this to resolve the problems with the different threads:
    appCallbacks.InvokeOnUIThread(new AppCallbackItem(() =>
    {

    adView.CacheAd();

    }), false);

    The full code is:
    void initVServ()
    {
    if (!Configuration.adsRemoved())
    {
    Debug.WriteLine("Ads Are not removed.");

    adView = new VservAdView();
    adView.UX = VservAdUX.Interstitial;
    adView.ZoneId = "XXXXXXXXXXX";
    //adView.TestDevice = new List<string>() { "" };
    adView.TimeOut = 10;
    adView.RequestedOrientation = RequestedOrientation.Landscape;
    isAdCached = false;

    adView.DidCacheAd += adView_DidCacheAd;
    adView.FailedToCacheAd += adView_FailedToCacheAd;

    //adView.CacheAd();

    SendShowAdMessage.showAdEvent += SendShowAdMessage_showAdEvent;
    }
    }

    void SendShowAdMessage_showAdEvent()
    {
    if (!Configuration.adsRemoved())
    {
    /*appCallbacks.InvokeOnUIThread(new AppCallbackItem(() =>
    {
    adView.LoadAd();
    }), true);*/

    if (isAdCached)
    {
    appCallbacks.InvokeOnUIThread(new AppCallbackItem(() =>
    {
    adView.ShowAd();
    adView.CacheAd();
    }), false);
    }
    else
    {
    appCallbacks.InvokeOnUIThread(new AppCallbackItem(() =>
    {
    try
    {
    adView.CacheAd();
    }
    catch (System.Exception e)
    {
    Debug.WriteLine(e.StackTrace);
    Debug.WriteLine(e.Message);
    }

    }), false);
    }

    }

    }

    void adView_FailedToCacheAd(object sender, EventArgs e)
    {
    Debug.WriteLine("Failed to Cache Ad - VServ");
    //adView.CacheAd();
    isAdCached = false;
    }

    void adView_DidCacheAd(object sender, EventArgs e)
    {
    Debug.WriteLine("Ad Cached - VServ");
    isAdCached = true;

    }

    Everything worked well that way except that I get AggregateException after the game returned from the UI thread and the cacheAd callback methods are called. When I sideload the app from visual studio there are no crashes because of this but after I download it from the windows phone store as beta app it crashes when I call the cacheAd method. I removed the VServ SDK, uploaded the game again and this time it doesn't crashed. I wonder if there is something that I missed about the way I handle the thread changes. What could be the reason for the game to crash only when it's downloaded from the store?

    Thanks in advance!
     
    Last edited: Mar 18, 2015
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,628
    Well, the error is pretty self explanatory:

    Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

    SendShowAdMessage.sendShowAdMessage() calls into the plugin from the wrong thread it seems.
     
  3. stamenkrumov

    stamenkrumov

    Joined:
    Jun 15, 2014
    Posts:
    33
    What I should do to call the methods in App.xaml.cs in the right thread?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,703
    It's not the file that matters. I think you should call it from UI thread.
    You can do this via AppCallbacks class or via Dispatcher, whichever is easier to access for you.
     
  5. stamenkrumov

    stamenkrumov

    Joined:
    Jun 15, 2014
    Posts:
    33
    I'm using :
    appCallbacks.InvokeOnUIThread(new AppCallbackItem(() =>
    {
    try
    {
    adView.CacheAd();
    }
    catch (System.Exception e)
    {
    Debug.WriteLine(e.StackTrace);
    Debug.WriteLine(e.Message);
    }

    }), false);

    But the aggregate exception and crashes when downloaded from store still exists.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,703
    Are you creating the asView itself on UI thread?
     
  7. stamenkrumov

    stamenkrumov

    Joined:
    Jun 15, 2014
    Posts:
    33
    Yes I do. I create it after the splash screen gets removed.
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,809
    Simply enable exceptions via Debug->Exceptions, and when this exception popsup, check the callstack.
     
  9. SirajUddin4757

    SirajUddin4757

    Joined:
    Mar 26, 2015
    Posts:
    1
    Hello stamenkrumov, i cant help u but you can help me, can u plz tell me how to implement admob in our own app? and how to call it when level complete or level fail.. ?? i would be very helpful for me