c#-5.0

Projection using async delegate/lambda

十年热恋 提交于 2019-12-30 13:11:45
问题 The following code will not compile against the Async CTP in Visual Studio 2010: Enumerable.Range(1, 5).Select(async x => { await TaskEx.Delay(100); return 5; }); The compilation error is as follows: Test.cs(40,13): error CS1928: 'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Select' and the best extension method overload 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' has some

Projection using async delegate/lambda

限于喜欢 提交于 2019-12-30 13:11:44
问题 The following code will not compile against the Async CTP in Visual Studio 2010: Enumerable.Range(1, 5).Select(async x => { await TaskEx.Delay(100); return 5; }); The compilation error is as follows: Test.cs(40,13): error CS1928: 'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Select' and the best extension method overload 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' has some

How to use async/await with a library that uses an event-based asynchronous pattern?

≯℡__Kan透↙ 提交于 2019-12-30 10:32:34
问题 I use a library that has an asynchronous method called DoWork(...) that will raise a WorkDone event when the operation completes. I would like to write a method that calls this library, but instead of maintaining the same pattern I would like my method to be async so it can be called with await . In essence, what I am trying to do is: public async Task<Result> SomeMethod() { var result = new Task<Result>(); library.WorkDone += (data) => { result.Result = data; } library.DoWork(); return await

What's the alternative to DataTable with ADO.NET under MVC 6?

折月煮酒 提交于 2019-12-30 06:42:08
问题 I am creating a MVC 6 project and I would rather use Classic ADO.net over Entity Framework 7. However It is saying that name space can not be found for both DataTable and SqlDataAdapter . I have a using System.Data and System.Data.SqlClient statement. It dose not show an error until I try to build the project. I think I read somewhere that those two names spaces are not implemented in the new version. If so is there an alternative way of doing it or am I missing a dependency or using

What are the differences between using ConfigureAwait(false) and Task.Run?

社会主义新天地 提交于 2019-12-29 10:14:45
问题 I understand that it's recommended to use ConfigureAwait(false) for await s in library code so that subsequent code does not run in the caller's execution context, which could be a UI thread. I also understand that await Task.Run(CpuBoundWork) should be used instead of CpuBoundWork() for the same reason. Example with ConfigureAwait public async Task<HtmlDocument> LoadPage(Uri address) { using (var client = new HttpClient()) using (var httpResponse = await client.GetAsync(address)

How to measure performance of awaiting asynchronous operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 08:39:06
问题 I have a Windows Service that reads from multiple MessageQueue instances. Those messagequeues all run their own Task for reading messages. Normally, after reading a message, the work of an I/O database is done. I've found articles claiming it's a good idea to use async on I/O operations, because it would free up threads. I'm trying to simulate the performance boost of using async I/O opertations in a Console application. The Console application In my test environment, I have 10 queues.

How do I convert this to an async task?

蹲街弑〆低调 提交于 2019-12-28 20:41:44
问题 Given the following code... static void DoSomething(int id) { Thread.Sleep(50); Console.WriteLine(@"DidSomething({0})", id); } I know I can convert this to an async task as follows... static async Task DoSomethingAsync(int id) { await Task.Delay(50); Console.WriteLine(@"DidSomethingAsync({0})", id); } And that by doing so if I am calling multiple times (Task.WhenAll) everything will be faster and more efficient than perhaps using Parallel.Foreach or even calling from within a loop. But for a

How do I convert this to an async task?

微笑、不失礼 提交于 2019-12-28 20:41:35
问题 Given the following code... static void DoSomething(int id) { Thread.Sleep(50); Console.WriteLine(@"DidSomething({0})", id); } I know I can convert this to an async task as follows... static async Task DoSomethingAsync(int id) { await Task.Delay(50); Console.WriteLine(@"DidSomethingAsync({0})", id); } And that by doing so if I am calling multiple times (Task.WhenAll) everything will be faster and more efficient than perhaps using Parallel.Foreach or even calling from within a loop. But for a

Why does WebClient.DownloadStringTaskAsync() block ? - new async API/syntax/CTP

假装没事ソ 提交于 2019-12-28 13:44:15
问题 For some reason there is a pause after the program below starts. I believe that WebClient().DownloadStringTaskAsync() is the cause. class Program { static void Main(string[] args) { AsyncReturnTask(); for (int i = 0; i < 15; i++) { Console.WriteLine(i); Thread.Sleep(100); } } public static async void AsyncReturnTask() { var result = await DownloadAndReturnTaskStringAsync(); Console.WriteLine(result); } private static async Task<string> DownloadAndReturnTaskStringAsync() { return await new

Unhandled exception handler not called for Metro / WinRT UI async void event handler

巧了我就是萌 提交于 2019-12-28 13:22:48
问题 Consider the following to be extracts from a Windows 8 Metro / WinRT app, which have been reduced to the bare minimum required to show the anomaly: public class App : Application { public App() { UnhandledException += (sender, e) => e.Handled = true; } } public class MainPage : Page { private void Button_Click_1(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } private async void Button_Click_2(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } }