c#-5.0

How can I use the async keywords in a project targeting.net 4.0

六眼飞鱼酱① 提交于 2019-11-26 17:46:51
问题 I would like to use the async keywords in a project that is created in .net 4.0. If I go to the nuget.org website and I look for "async", i many results, but mainly I get this: Visual Studio Async CTP (Version 3, Unofficial) 0.3.0 AsyncAwaitCTP 1.0.0 Which is the differences between both of them? 回答1: You want the Microsoft.Bcl.Async package. That's a properly released, non-CTP package that is stable. This also requires VS2012 since an updated compiler is needed to understand async and await

How to implement INotifyPropertyChanged in C# 6.0?

ぐ巨炮叔叔 提交于 2019-11-26 17:46:11
问题 The answer to this question has been edited to say that in C# 6.0, INotifyPropertyChanged can be implemented with the following OnPropertyChanged procedure: protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } However, it isn't clear from that answer what the corresponding property definition should be. What does a complete implementation of INotifyPropertyChanged look like in C# 6.0 when

Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?

二次信任 提交于 2019-11-26 16:11:14
问题 In .NET 4.5 / C# 5, IReadOnlyCollection<T> is declared with a Count property: public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable { int Count { get; } } I am wondering, wouldn't it have made sense for ICollection<T> to implement the IReadOnlyCollection<T> interface as well: public interface ICollection<T> : IEnumerable<T>, IEnumerable, *IReadOnlyCollection<T>* This would've meant that classes implementing ICollection<T> would've automatically implemented

What&#39;s the new C# await feature do? [closed]

可紊 提交于 2019-11-26 15:51:43
Can anyone explain what the await function does? RTigger They just talked about this at PDC yesterday! Await is used in conjunction with Tasks (parallel programming) in .NET. It's a keyword being introduced in the next version of .NET. It more or less lets you "pause" the execution of a method to wait for the Task to complete execution. Here's a brief example: //create and run a new task Task<DataTable> dataTask = new Task<DataTable>(SomeCrazyDatabaseOperation); //run some other code immediately after this task is started and running ShowLoaderControl(); StartStoryboard(); //this will actually

call async method without await #2

試著忘記壹切 提交于 2019-11-26 15:45:28
问题 I have an async method: public async Task<bool> ValidateRequestAsync(string userName, string password) { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); string stringResponse = await response.Content.ReadAsStringAsync(); return bool.Parse(stringResponse); } } I call this method like this: bool isValid = await ValidateRequestAsync("user1", "pass1"); Can i call the same method from an synchronous method, without using await keyword? Ex:

async - stay on the current thread?

坚强是说给别人听的谎言 提交于 2019-11-26 15:28:51
I've read Eric lippert's article about async , and about confusions people had with async keyword. he said : it ( async ) means “this method contains control flow that involves awaiting asynchronous operations and will therefore be rewritten by the compiler into continuation passing style to ensure that the asynchronous operations can resume this method at the right spot.” The whole point of async methods it that you stay on the current thread as much as possible I don't understand this. If I execute an asynchronous method ( Task ) and it runs , it surely runs on another thread. Moreover , If

Best way to convert callback-based async method to awaitable task

孤者浪人 提交于 2019-11-26 15:13:00
问题 What would be the best way to convert/wrap a "classic" asynchronous method that uses a callback to something that returns a (awaitable) Task? For example, given the following method: public void GetStringFromUrl(string url, Action<string> onCompleted); The only way I know of to wrap this into a method returning a task is: public Task<string> GetStringFromUrl(string url) { var t = new TaskCompletionSource<string>(); GetStringFromUrl(url, s => t.TrySetResult(s)); return t.Task; } Is this the

Do you have to put Task.Run in a method to make it async?

回眸只為那壹抹淺笑 提交于 2019-11-26 14:49:42
I'm trying to understand async await in the simplest form. I want to create a very simple method that adds two numbers for the sake of this example, granted, it's no processing time at all, it's just a matter of formulating an example here. Example 1 private async Task DoWork1Async() { int result = 1 + 2; } Example 2 private async Task DoWork2Async() { Task.Run( () => { int result = 1 + 2; }); } If I await DoWork1Async() will the code run synchronously or asynchronously? Do I need to wrap the sync code with Task.Run to make the method awaitable AND asynchronous so as not to block the UI thread

I want await to throw AggregateException, not just the first Exception

柔情痞子 提交于 2019-11-26 12:43:32
问题 When awaiting a faulted task (one that has an exception set), await will rethrow the stored exception. If the stored exception is an AggregateException it will rethrow the first and discard the rest. How can we use await and at the same time throw the original AggregateException so that we do not accidentally lose error information? Note, that it is of course possible to think of hacky solutions for this (e.g. try-catch around the await , then call Task.Wait ). I really wish to find a clean

Why can&#39;t I catch an exception from async code?

半世苍凉 提交于 2019-11-26 12:28:52
问题 Everywhere I read it says the following code should work, but it doesn\'t. public async Task DoSomething(int x) { try { // Asynchronous implementation. await Task.Run(() => { throw new Exception(); x++; }); } catch (Exception ex) { // Handle exceptions ? } } That said, I\'m not catching anything and get an \"unhandled exception\" originating at the \'throw\' line. I\'m clueless here. 回答1: You have the "Just my code" Option turned on. With this on, it is considering the exception unhandled