public async Task StartLongRunningTask(FunctionPassingToNativeBackground obj)
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var        tmp    = obj.ClassObject;
                Type       type   = tmp.GetType();
                MethodInfo method = type.GetMethod(obj.FunctionName);
                await(Task) method.Invoke(tmp, obj.Params.ToArray());
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
Ejemplo n.º 2
0
        void WireUpLongRunningTask()
        {
            MessagingCenter.Subscribe <StartLongRunningTaskMessage, FunctionPassingToNativeBackground>(this, "StartLongRunningTaskMessage", (message, obj) =>
            {
                var intent = new Intent(this, typeof(LongRunningTask));
                FuncInfo   = obj;

                FunctionPasserToNativeBackground.FunctionName = FuncInfo.FunctionName;
                FunctionPasserToNativeBackground.Params       = FuncInfo.Params;
                FunctionPasserToNativeBackground.ClassObject  = FuncInfo.ClassObject;
                StartService(intent);
            });
        }