int input = 10;
var cancelTokenSource = new CancellationTokenSource();
var task = new Task<int>(() => TestF(input, cancelTokenSource.Token), cancelTokenSource.Token);
task.Start();
cancelTokenSource.Cancel();
Debug.LogFormat("Cancel!");
Debug.LogFormat("Task Cancel : {0}", task.IsCanceled);
public int TestF(int p, CancellationToken token)
{
if (p <= 0)
throw new InvalidOperationException("Can not calculate by input data.");
int count = 1;
for (var i = 1; i <= p; i++)
{
if (token.IsCancellationRequested)
{
throw new OperationCanceledException(token);
return count;
}
count = count + i;
Debug.Log(count);
Thread.Sleep(100);
}
return n;
}
CancellationTokenSource 생성
IsCancellationRequested 을 체크해서 종료.
'Unity > 기타' 카테고리의 다른 글
Dotween (0) | 2019.10.21 |
---|---|
Debug Log 에 칼라 넣기 (0) | 2019.10.11 |
TextMeshProUGUI 에서 한글 폰트 만들기 (0) | 2019.09.18 |
Task async awit 로 쓰레드 사용하기 (0) | 2019.09.17 |
쓰레드 풀 & async ask 사용하기 (0) | 2019.09.17 |