c#-5.0

C# 5.0 async await return a list

别来无恙 提交于 2019-12-03 15:49:10
I'm learning about async/await, and ran into a situation where I need to call an async method that should return an object or list of same object. Is this the right way to implement ? from AManager.cs public async Task Initialize(string objectPath) { AnObject someObject = await BClass.GetAnObject(objectPath); } and this is the called method Class B: public async Task<AnObject> GetAnObject(string objectPath) { AnObject someObj = new AnObject(); return someObj; } What happens if I want to return a list of object ? I should create a wrapper that contains a list ? and return that wrapper ? Because

Async Await targeting 4.0 deployment requirements

送分小仙女□ 提交于 2019-12-03 15:17:16
Microsoft has updated the async/await targeting for .net 4.0 and now suggests using the Microsoft.Bcl.Async library available on nuget . In the release notes, it states that .net 4 with KB 2468871 is required. Is KB2468871 a build requirement or a deployment requirement? What aspect of KB2468871 makes it required? Quoting from http://support.microsoft.com/kb/2468871/en-us Feature 5 Changes to the support portable libraries. These changes include API updates and binder modifications. This update enables the CLR to bind successfully to portable libraries so that a single DLL can run on the .NET

async / await or Begin / End with TcpListener?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 14:31:06
I've started to build a tcp server which will be able to accept many clients, and receive simultaneously from all of the clients new data. Until now, I used IOCP for tcp servers which was pretty easy and comfortable, but this time I want to use the Async / Await tech. that was released in C# 5.0. The problem is that when I started to write the server using async / await, I figured out that in tcp multiple users server use case, async / await tech. and the regular synchrony methods will work the same. Here's a simple example to be more specific: class Server { private TcpListener _tcpListener;

How to attach CancellationTokenSource to DownloadStringTaskAsync method and cancel the async call?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:56:01
问题 I an creating a sample example to call link using WebClient using async and await method now I want to attach cancel async call functionality also. But I am not able to get CancellationTokenSource token and attach DownloadStringTaskAsync to this cancellation token. Following Is my code can anyone tell me how to accomplish this. private async void DoWork() { this.Cursor = Cursors.WaitCursor; Write("DoWork started."); cts = new CancellationTokenSource(); WebClient wc = new WebClient(); string

TcpListener: how to stop listening while awaiting AcceptTcpClientAsync()?

此生再无相见时 提交于 2019-12-03 11:35:26
问题 I don't know how to properly close a TcpListener while an async method await for incoming connections. I found this code on SO, here the code : public class Server { private TcpListener _Server; private bool _Active; public Server() { _Server = new TcpListener(IPAddress.Any, 5555); } public async void StartListening() { _Active = true; _Server.Start(); await AcceptConnections(); } public void StopListening() { _Active = false; _Server.Stop(); } private async Task AcceptConnections() { while (

The awaitable and awaiter In C# 5.0 Asynchronous

跟風遠走 提交于 2019-12-03 11:27:08
Task or Task<TResult> object is awaitable, so we can use await key on those whose return value is Task or Task<TResult>. Task or Task<TResult> are the most frequently-used awaitable object. We also can define our own awaitable object.The object should has below qualification. It has a GetAwaiter() method (instance method or extension method); Its GetAwaiter() method returns an awaiter. An object is an awaiter if: It implements INotifyCompletion or ICriticalNotifyCompletion interface; It has an IsCompleted, which has a getter and returns a Boolean; it has a GetResult() method, which returns

Await on the last method line

那年仲夏 提交于 2019-12-03 11:21:27
问题 Still learning about async-await. I bumped into examples similar to following: public async Task MethodAsync() { await Method01Async(); await Method02Async(); } What is the purpose of the last await? Method02Async is the last line of MethodAsync method. So there is no any method remainder - no any lines below - no anything to be called in the callback generated by the compiler... Am I missing anything? 回答1: There actually is a "method remainder" - it completes the Task returned by MethodAsync

C# 5.0 async/await feature and Rx - Reactive Extensions

萝らか妹 提交于 2019-12-03 06:30:21
问题 I am wondering what do the new C# 5.0 asynchronous features mean for Rx - Reactive Extensions? It seems to be not a replacement but they seem to overlap - Task and IObservable . 回答1: Check also: TPL Dataflow Overview about TDF and Rx: Astute readers may notice some similarities between TPL Dataflow and Reactive Extensions (Rx), currently available as a download from the DevLabs prototypes site. Rx is predominantly focused on coordination and composition of event streams with a LINQ-based API,

Why does TaskCanceledException occur?

北战南征 提交于 2019-12-03 06:28:28
问题 I have the following test code: void Button_Click(object sender, RoutedEventArgs e) { var source = new CancellationTokenSource(); var tsk1 = new Task(() => Thread1(source.Token), source.Token); var tsk2 = new Task(() => Thread2(source.Token), source.Token); tsk1.Start(); tsk2.Start(); source.Cancel(); try { Task.WaitAll(new[] {tsk1, tsk2}); } catch (Exception ex) { // here exception is caught } } void Thread1(CancellationToken token) { Thread.Sleep(2000); // If the following line is enabled,

When to use OrderByCompletion (Jon Skeet) vs Parallel.ForEach with async delegates

允我心安 提交于 2019-12-03 04:32:12
Recently Jon Skeet at NDC London spoke about C# 5 async/await and presented the idea of " ordering by completion " a list of async tasks. A link http://msmvps.com/blogs/jon_skeet/archive/2012/01/16/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx I am a bit confused or should I say I am not sure when will this technique be more appropriate to use. I cannot understand the difference between this and the below example var bag = new ConcurrentBag<object>(); Parallel.ForEach(myCollection, async item => { // some pre stuff var response = await GetData(item); bag.Add(response); // some