0

This is the code I run. In the simulator the code runs. But on the device as soon as I hide the app the code pauses and the Task.Delay is never done and the second Clipboard set as well.

await Clipboard.SetTextAsync(text); 
await Task.Delay(5000,_cancellationTokenSource.Token); 
await Clipboard.SetTextAsync(string.Empty);

How could I execute this code when then user has switched to a different application?

nikogram
  • 71
  • 6
  • 1
    google `xamarin forms run code when app is in background`. – ToolmakerSteve Dec 21 '22 at 23:43
  • 2
    There are numerous existing posts about bg processing on mobile devices – Jason Dec 21 '22 at 23:45
  • What do you mean with "the Task.Delay is never done"? Could you share with us your code for further investigation on this issue? – Ai Zhang-MSFT Dec 22 '22 at 07:39
  • @AiZhang-MSFT I have attached the code. These 3 lines is all I do. If I hide the app during the app executes the Task.Delay then the third line will not run. The thread is somehow blocked. – nikogram Dec 22 '22 at 08:58
  • Sure there are plenty of examples running code in background but this is not what I want. I am not executing asynchronous code or in intervals a piece of code. I run this code and all I want to do is to complete the execution of these 3 lines even if the user has minimised the app. – nikogram Dec 22 '22 at 09:07
  • You could check this [link](https://stackoverflow.com/questions/60515621/why-does-my-background-worker-deadlock-when-using-task-delay) for more information. Hope it could help you. – Ai Zhang-MSFT Dec 23 '22 at 09:37

0 Answers0