A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hello @Giorgio Sfiligoi ,
Thanks for your question.
A .NET Task<Result> and an Android IListenableFuture are completely different types. You cannot cast one to the other, even though the compiler allows it, Android throws InvalidCastException at runtime.
It is highly likely that the error is coming from this line:
return (IListenableFuture)taskCompletionSource.Task;
I recommend using Simple Worker instead. Just switch from ListenableWorker to the regular Worker class and block the async call:
using Android.Content;
using AndroidX.Work;
using System.Diagnostics;
namespace Draft4.Platforms.Android.Workers
{
public class OnSleepWorker : Worker
{
public const string TAG = "OnSleepWorker";
public OnSleepWorker(Context context, WorkerParameters workerParams)
: base(context, workerParams) { }
public override Result DoWork()
{
try
{
Draft4.Services.Document.OnSleep()
.GetAwaiter()
.GetResult();
Debug.WriteLine("OnSleepWorker succeeded");
return Result.InvokeSuccess()!;
}
catch (Exception ex)
{
Debug.WriteLine($"OnSleepWorker failed: {ex.Message}");
return Result.InvokeFailure()!;
}
}
}
}
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.