Forum Xamarin.Android

Background process raised error.

below is my code which I took from Xamarin code sample.

btnCheckProviderCode.Click += delegate
{
progress = ProgressDialog.Show(this, "Loading", "Please Wait...", true);
Task.Factory.StartNew(
() =>
{
LongRunningProcess(4);
}
).ContinueWith(
t =>
{
if (progress != null)
progress.Hide();
}, TaskScheduler.FromCurrentSynchronizationContext()
);
};

protected void LongRunningProcess(int seconds)
{
System.Threading.Thread.Sleep(seconds * 1000);
}

But when I run...it gives me below error.

Unhandled Exception:
Java.Lang.NullPointerException:

Thanks in advance.

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    In the ContinueWith try using

    t => 
    {
        RunOnUiThread(() =>
        {
            if(progress != null)
                progress.Hide();
        }
    }
    
  • abcd99987abcd99987 INMember

    Please see attached screen shot about error.

    This solution does not working. because error is not related to UI access. It event does not entering t=> block.

    Thanks

  • SKallSKall USMember ✭✭✭✭

    Which Xamarin example project is this? Can you also post the code for LongRunningProcess? It does look like the error is coming from there. Just to rule it out, I would surround it with try-catch and run again.

    try
    {
    LongRunningProcess(4);
    }
    catch (Java.Lang.NullPointerException ex)
    {
    // breakpoint here
    }
    

  • abcd99987abcd99987 INMember

    This sample is multithreading sample...there is nothing in that LongRunningProcess() function, I mentioned in my original query.
    If I remove Task process and put it in direct onCreate method..it run sucessfully.

    Sample: https://github.com/xamarin/mobile-samples/tree/master/MultiThreading

    This sample gives me java.lang.null error.
    But if I remove Task.Factory.StartNew...and run that LongRun function directly..it works fine.

    Thanks

Sign In or Register to comment.