Posts

Showing posts with the label async-await

async Task() not returning straight away when not using await

Image
Clash Royale CLAN TAG #URR8PPP async Task<T>() not returning straight away when not using await If I have the following function public async Task<bool> Foo() { // call many async functions await Bar1().ConfigureAwait(false); await Bar2().ConfigureAwait(false); return await Bar3().ConfigureAwait(false); } If I call the function above var t1 = Foo(); var t2 = Foo(); ... await Task.WhenAll(t1, t2).ConfigureAwait(false); t2 will not execute until t1 completes, (so calling WaitAll is a bit pointless). WaitAll But if I call ... var t1 = Task.Run( async () => await Foo().ConfigureAwait(false)); var t2 = Task.Run( async () => await Foo().ConfigureAwait(false)); ... await Task.WhenAll(t1, t2).ConfigureAwait(false); t1 and t2 return immediately... And now if I run ... ... var t1 = Task.Run( () => Foo()); var t2 = Task.Run( () => Foo()); ... await Task.WhenAll(t1, t2).ConfigureAwait(false); t1 and t2 also return immediately... Why does Foo() not return task immed...

HttpPost does not get ContentLength from PostAsync request

Image
Clash Royale CLAN TAG #URR8PPP HttpPost does not get ContentLength from PostAsync request I have a unit test code that sends request to an HttpPost method. [Test] public async Task ValidateToken() { var content = new FormUrlEncodedContent(new { new KeyValuePair<string, string("test", "test") }; httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var response = await httpClient.PostAsync("/api/token/1", content); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); } [Route("api/token")] public class MyController : BaseApiController { private readonly HttpRequest httpRequest; public MyController(IHttpContextAccessor httpContextAccessor) { httpRequest = httpContextAccessor.HttpContext.Request; } [HttpPost("{count}")] public async Task<IActionResult> Post(int count) { if...

Multithread Pathfinding, Unit can't receive path array result

Image
Clash Royale CLAN TAG #URR8PPP Multithread Pathfinding, Unit can't receive path array result I have a pathfinder program using A* that i made following a tutorial. It works fine until i tried to make it multithread using async Task. The Thread works and the path is calculated, but when the path result is being passed to the Unit object, for some resaon only one or two Unit that receive the path array. where did i do wrong ? RequestManager : public class ThreadRequestManager : MonoBehaviour { Queue<PathResult> results = new Queue<PathResult>(); static ThreadRequestManager instance; ThreadPathfinding pathfinding; private void Awake() { instance = this; pathfinding = GetComponent<ThreadPathfinding>(); } private void Update() { if (results.Count > 0) { int itemsInQueue = results.Count; lock (results) { for(int i = 0; i < itemsInQueue; i++) { PathResult result = results.Dequeue(); ...