taskcompletionsource

WP8 TaskCompletionSource not getting Result

这一生的挚爱 提交于 2020-01-14 06:31:10
问题 I've an extension method for WebClient (WP8) public static Task<string> DownloadStringTask(this WebClient webClient, Uri uri) { var tcs = new TaskCompletionSource<string>(); webClient.DownloadStringCompleted += (s, e) => { if (e.Error != null) { tcs.TrySetException(e.Error); } else if (e.Cancelled) { tcs.TrySetCanceled(); } else { tcs.TrySetResult(e.Result); } }; webClient.DownloadStringAsync(uri); return tcs.Task; } and the call to this method public string GetResult() { var task = new

TaskCompletionSource throws “An attempt was made to transition a task to a final state when it had already completed”

醉酒当歌 提交于 2020-01-12 04:30:11
问题 I want to use TaskCompletionSource to wrap MyService which is a simple service: public static Task<string> ProcessAsync(MyService service, int parameter) { var tcs = new TaskCompletionSource<string>(); //Every time ProccessAsync is called this assigns to Completed! service.Completed += (sender, e)=>{ tcs.SetResult(e.Result); }; service.RunAsync(parameter); return tcs.Task; } This code is working well for the first time. But the second time I call ProcessAsync simply the event handler for the

How to cancel a TaskCompletionSource using a timout

痞子三分冷 提交于 2019-12-22 04:12:12
问题 I have the function that I call asynchronously using the await key word: public Task<StatePropertyEx> RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource<StateInfo>(); try { var propInstance = BuildCacheKey(entity, propName); StateCacheItem cacheItem; if (_stateCache.TryGetValue(propInstance, out cacheItem)) { tcs.SetResult( new StateInfo (cacheItem.State.Name, cacheItem.State.Value) ); return tcs.Task; } //state not found in local cache so save the

How to cancel a TaskCompletionSource using a timout

馋奶兔 提交于 2019-12-22 04:11:04
问题 I have the function that I call asynchronously using the await key word: public Task<StatePropertyEx> RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource<StateInfo>(); try { var propInstance = BuildCacheKey(entity, propName); StateCacheItem cacheItem; if (_stateCache.TryGetValue(propInstance, out cacheItem)) { tcs.SetResult( new StateInfo (cacheItem.State.Name, cacheItem.State.Value) ); return tcs.Task; } //state not found in local cache so save the

Timeout an async method implemented with TaskCompletionSource

*爱你&永不变心* 提交于 2019-12-17 17:57:07
问题 I have a blackbox object that exposes a method to kick of an async operation, and an event fires when the operation is complete. I have wrapped that into an Task<OpResult> BlackBoxOperationAysnc() method using TaskCompletionSource - that works well. However, in that async wrapper I'd like to manage completing the async call with a timeout error if the event is not received after a given timeout. Currently I manage it with a timer as: public Task<OpResult> BlackBoxOperationAysnc() { var tcs =

How to cancel a async task that starts a process in C#?

戏子无情 提交于 2019-12-11 06:18:43
问题 Currently my code runs a checkout from svn and redirections the stdout and stderr to a textbox using two tasks as seen below. I want to be able to cancel the task immediately when a user clicks the StopButton and be able to cancel the download. I understand that if I changed my cmd.exe command to say something such as "Pause", which continues to run until a user clicks something, I can cancel this command with the StopButton. I am very new to C# and trying to understand why I can cancel that

How to cancel a TaskCompletionSource using a timout

随声附和 提交于 2019-12-05 01:33:15
I have the function that I call asynchronously using the await key word: public Task<StatePropertyEx> RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource<StateInfo>(); try { var propInstance = BuildCacheKey(entity, propName); StateCacheItem cacheItem; if (_stateCache.TryGetValue(propInstance, out cacheItem)) { tcs.SetResult( new StateInfo (cacheItem.State.Name, cacheItem.State.Value) ); return tcs.Task; } //state not found in local cache so save the tcs for later and request the state var cacheKey = BuildCacheKey(entity, propName);

TaskCompletionSource throws “An attempt was made to transition a task to a final state when it had already completed”

浪尽此生 提交于 2019-12-03 05:32:16
I want to use TaskCompletionSource to wrap MyService which is a simple service: public static Task<string> ProcessAsync(MyService service, int parameter) { var tcs = new TaskCompletionSource<string>(); //Every time ProccessAsync is called this assigns to Completed! service.Completed += (sender, e)=>{ tcs.SetResult(e.Result); }; service.RunAsync(parameter); return tcs.Task; } This code is working well for the first time. But the second time I call ProcessAsync simply the event handler for the Completed is assign again (the same service variable is used every time) and thus it will execute twice

What is the purpose of TaskCreationOptions with a TaskCompletionSource?

若如初见. 提交于 2019-12-01 02:57:21
There's something unclear to me about the inner workings of TaskCompletionSource<> . When creating a simple Task<> using the Factory , I expect this task to be enqueued in a thread pool, unless I specify TaskCreationOptions.LongRunning , where it will run in a new thread instead. My understanding of TaskCompletionSource , is that I am responsible of triggering when a task ends, or fails, and I have full control on how to manage threads. However, the ctor of TaskCompletionSource allows me to specify a TaskCreationOptions , and this confuse me, since I was expecting the Scheduler not being able

What is the purpose of TaskCreationOptions with a TaskCompletionSource?

徘徊边缘 提交于 2019-11-30 23:00:57
问题 There's something unclear to me about the inner workings of TaskCompletionSource<> . When creating a simple Task<> using the Factory , I expect this task to be enqueued in a thread pool, unless I specify TaskCreationOptions.LongRunning , where it will run in a new thread instead. My understanding of TaskCompletionSource , is that I am responsible of triggering when a task ends, or fails, and I have full control on how to manage threads. However, the ctor of TaskCompletionSource allows me to