私はxamarinフォームPCL + iOSで作業しています。バックグラウンドに入ったときにタスクを取り消したいアプリがフォアグラウンドに入ると、それが始まります。アプリがバックグラウンドに入ったときのタスクキャンセル
これは私がこれまでに試したことです。もし私の方法がどんな仕事を取り消すのか、それとも何が起こっているのか分かりません。
async void getData()
{
bool isSuccess = await getSomeData();
if(isSuccess)
await getSomeMoreData();
}
CancellationTokenSource cts;
async Task<bool> getSomeData()
{
cts = new CancellationTokenSource();
AppEntersBackgorund += (sender,args) => { cts. cancel();});
CancellationToken token = new CancellationToken();
token = cts.token;
await Task.Run(() => {
token.ThrowIfCancellationRequested();
isSuccess = ParserData(token); // parsedata also checks periodically if task is cancelled
},token); //what happens here when cancel called?
return isSuccess;
}
async void getSomeMoreData()
{
if(!cts.IsCancellationRequested)
cts = new CancellationTokenSource();
AppEntersBackgorund += (sender,args) => { cts. cancel();});
CancellationToken token = new CancellationToken();
token = cts.token;
await Task.Run(() =>
{
token.ThrowIfCancellationRequested();
ParseSomeMoreData(token);
},token);
}
アプリがforegorundに入ると、もう一度getData()メソッドを呼び出して、私がもう一度やり直すようにします。
タスクはキャンセルされず、getSomeMoreDataは2回呼び出されます(または、アプリケーションがバックグラウンドからフォアグラウンドに移動する回数)。
私はこれをどのように達成できるのか誰かが説明できますか?そして、ここで何が起こっていますか?