Bug 29158 - XF for WP8.1RT - BeginInvokeOnMainThread generates NullReferenceException
Summary: XF for WP8.1RT - BeginInvokeOnMainThread generates NullReferenceException
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 1.4.2
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
: 29238 ()
Depends on:
Blocks:
 
Reported: 2015-04-16 12:45 UTC by Phil Russell
Modified: 2015-04-28 04:13 UTC (History)
8 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Case (298.44 KB, application/zip)
2015-04-16 15:31 UTC, John Miller [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Phil Russell 2015-04-16 12:45:39 UTC
I ran into problems while trying to use BeginInvokeOnMainThread with the XF for Windows Preview on Windows Phone 8.1 (Runtime). I can duplicate the problem with a simple contrived example. I added the following code to the BasicListPage constructor in Xamarin's WorkingWithListview example:

            System.Threading.Tasks.Task.Run(async () =>
            {
                await System.Threading.Tasks.Task.Delay(5000);
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => DisplayAlert("Time's up", "", "OK"));
            });

On iOS, Android, and Windows Phone 8.0 Silverlight, the code works as expected: the alert box is displayed 5 seconds after the app starts up. On Windows Phone 8.1 RT, however, this exception is generated instead:


  System.NullReferenceException occurred
  _HResult=-2147467261
  _message=Object reference not set to an instance of an object.
  HResult=-2147467261
  IsTransient=false
  Message=Object reference not set to an instance of an object.
  Source=Xamarin.Forms.Platform.WinRT
  StackTrace:
       at Xamarin.Forms.Platform.WinRT.WindowsBasePlatformServices.BeginInvokeOnMainThread(Action action)
  InnerException: 

The call stack looks like this:
    Xamarin.Forms.Platform.WinRT.DLL!Xamarin.Forms.Platform.WinRT.WindowsBasePlatformServices.BeginInvokeOnMainThread(System.Action action = {System.Action})   Unknown
    Xamarin.Forms.Core.DLL!Xamarin.Forms.Device.BeginInvokeOnMainThread(System.Action action = {System.Action}) Unknown
>   WorkingWithListview.DLL!WorkingWithListview.BasicListPage..ctor() Line 24   C#
    [Resuming Async Method] 
    mscorlib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)   Unknown
    mscorlib.ni.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)    Unknown
    mscorlib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)    Unknown
    mscorlib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() Unknown
    mscorlib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()  Unknown
    mscorlib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() Unknown
    mscorlib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()    Unknown
    mscorlib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() Unknown
    mscorlib.ni.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask = null)  Unknown
    mscorlib.ni.dll!System.Threading.Tasks.Task.FinishContinuations()   Unknown
    mscorlib.ni.dll!System.Threading.Tasks.Task.FinishStageThree()  Unknown
    mscorlib.ni.dll!System.Threading.Tasks.Task.TrySetResult(System.Threading.Tasks.VoidTaskResult result)   Unknown
    mscorlib.ni.dll!System.Threading.Tasks.Task.DelayPromise.Complete() Unknown
    mscorlib.ni.dll!System.Threading.Tasks.Task.Delay.AnonymousMethod__19(object state) Unknown
    mscorlib.ni.dll!System.Threading.TimerQueueTimer.CallCallbackInContext(object state)    Unknown
    mscorlib.ni.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)    Unknown
    mscorlib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)    Unknown
    mscorlib.ni.dll!System.Threading.TimerQueueTimer.CallCallback() Unknown
    mscorlib.ni.dll!System.Threading.TimerQueueTimer.Fire() Unknown
    mscorlib.ni.dll!System.Threading.TimerQueue.FireNextTimers()    Unknown
    mscorlib.ni.dll!System.Threading.TimerQueue.AppDomainTimerCallback()    Unknown
Comment 1 Phil Russell 2015-04-16 13:18:14 UTC
Sorry...the snippet of code was added to the BasicListView constructor in the WorkingWithListView example here: https://github.com/conceptdev/xamarin-forms-windows-preview/blob/master/WorkingWithListview/WorkingWithListview/Basic/BasicListPage.cs
Comment 2 John Miller [MSFT] 2015-04-16 15:31:21 UTC
Created attachment 10795 [details]
Test Case
Comment 3 Matthew Leibowitz 2015-04-16 17:09:42 UTC
Another way that create this error is to update a bound property, on the view model, from a background thread.
Comment 4 Jason Smith [MSFT] 2015-04-17 19:24:45 UTC
Should be fixed in 1.4.3-pre1
Comment 5 Eric Maupin 2015-04-20 12:48:35 UTC
*** Bug 29238 has been marked as a duplicate of this bug. ***
Comment 6 Parmendra Kumar 2015-04-28 04:13:24 UTC
I have checked this issue with Xamarin.Forms 1.4.3-pre2 , Its working fine at my end.


Hence closing this issue.