call webRequest.Abort() on canceled

master
neuecc 2020-05-25 09:54:26 +09:00
parent 8a56838111
commit 7cce0f48e5
2 changed files with 30 additions and 19 deletions

View File

@ -13,7 +13,7 @@ namespace Cysharp.Threading.Tasks
{ {
public static partial class UnityAsyncExtensions public static partial class UnityAsyncExtensions
{ {
#region AsyncOperation #region AsyncOperation
public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation) public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation)
{ {
@ -116,7 +116,7 @@ namespace Cysharp.Threading.Tasks
try try
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.GetResult(token); core.GetResult(token);
} }
finally finally
@ -179,9 +179,9 @@ namespace Cysharp.Threading.Tasks
} }
} }
# endregion #endregion
#region ResourceRequest #region ResourceRequest
public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperation) public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperation)
{ {
@ -288,7 +288,7 @@ namespace Cysharp.Threading.Tasks
try try
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
return core.GetResult(token); return core.GetResult(token);
} }
finally finally
@ -356,9 +356,9 @@ namespace Cysharp.Threading.Tasks
} }
} }
# endregion #endregion
#region AssetBundleRequest #region AssetBundleRequest
public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest asyncOperation) public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest asyncOperation)
{ {
@ -465,7 +465,7 @@ namespace Cysharp.Threading.Tasks
try try
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
return core.GetResult(token); return core.GetResult(token);
} }
finally finally
@ -533,9 +533,9 @@ namespace Cysharp.Threading.Tasks
} }
} }
# endregion #endregion
#region AssetBundleCreateRequest #region AssetBundleCreateRequest
public static AssetBundleCreateRequestAwaiter GetAwaiter(this AssetBundleCreateRequest asyncOperation) public static AssetBundleCreateRequestAwaiter GetAwaiter(this AssetBundleCreateRequest asyncOperation)
{ {
@ -642,7 +642,7 @@ namespace Cysharp.Threading.Tasks
try try
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
return core.GetResult(token); return core.GetResult(token);
} }
finally finally
@ -710,10 +710,10 @@ namespace Cysharp.Threading.Tasks
} }
} }
# endregion #endregion
#if ENABLE_UNITYWEBREQUEST #if ENABLE_UNITYWEBREQUEST
#region UnityWebRequestAsyncOperation #region UnityWebRequestAsyncOperation
public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation) public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation)
{ {
@ -820,7 +820,7 @@ namespace Cysharp.Threading.Tasks
try try
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
return core.GetResult(token); return core.GetResult(token);
} }
finally finally
@ -853,6 +853,7 @@ namespace Cysharp.Threading.Tasks
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
asyncOperation.webRequest.Abort();
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
return false; return false;
} }
@ -888,7 +889,7 @@ namespace Cysharp.Threading.Tasks
} }
} }
# endregion #endregion
#endif #endif
} }

View File

@ -252,8 +252,18 @@ public class SandboxMain : MonoBehaviour
async UniTaskVoid Test2() async UniTaskVoid Test2()
{ {
var r = await UniAsync("https://bing.com/"); try
Debug.Log("UNIASYNC"); {
var cts = new CancellationTokenSource();
var r = UniAsync("https://bing.com/", cts.Token);
cts.Cancel();
await r;
Debug.Log("UNIASYNC");
}
catch
{
Debug.Log("Canceled");
}
} }
IEnumerator Test3(string url) IEnumerator Test3(string url)
@ -269,9 +279,9 @@ public class SandboxMain : MonoBehaviour
return req; return req;
} }
static async UniTask<UnityWebRequest> UniAsync(string url) static async UniTask<UnityWebRequest> UniAsync(string url, CancellationToken cancellationToken)
{ {
var req = await UnityWebRequest.Get(url).SendWebRequest(); var req = await UnityWebRequest.Get(url).SendWebRequest().WithCancellation(cancellationToken);
return req; return req;
} }