c#-5.0

multiple parallel async calls with await

血红的双手。 提交于 2019-11-28 17:48:25
As far as I know, when runtime comes across the statement below it wraps the rest of the function as a callback to the method which is invoked asynchronously ( someCall() in this example). In this case anotherCall() will be executed as a callback to someCall() : await someCall(); await anotherCall(); I wonder if it is possible to make runtime perform like this: call someCall() in async fashion and return immediately to the calling thread, then invoke anotherCall() similarly (without waiting someCall to complete). Because I need these two methods to run asynchronously and suppose these calls

Debugging exceptions in a Async/Await (Call Stack)

浪子不回头ぞ 提交于 2019-11-28 17:09:42
问题 I use the Async/Await to free my UI-Thread and accomplish multithreading. Now I have a problem when I hit a exception. The Call Stack of my Async parts allways starts with ThreadPoolWorkQue.Dipatch() , which doesn't help me very much. I found a MSDN-Article Andrew Stasyuk. Async Causality Chain Tracking about it but as I understand it, its not a ready to use solution. What is the best/easiest way to debug if you use multithreading with Async/Await? 回答1: The article you found does a good job

Architecture for async/await

怎甘沉沦 提交于 2019-11-28 13:51:20
问题 If you are using async/await at a lower level in your architecture, is it necessary to "bubble up" the async/await calls all the way up, is it inefficient since you are basically creating a new thread for each layer (asynchronously calling an asynchronous function for each layer, or does it not really matter and is just dependent on your preference? I'm using EF 6.0-alpha3 so that I can have async methods in EF. My repository is such: public class EntityRepository<E> : IRepository<E> where E

Does async and await increase performance of an ASP.Net application

我是研究僧i 提交于 2019-11-28 13:19:22
I recently read an article about c#-5 and new & nice asynchronous programming features . I see it works greate in windows application. The question came to me is if this feature can increase ASP.Net performance? consider this two psudo code: public T GetData() { var d = GetSomeData(); return d; } and public async T GetData2() { var d = await GetSomeData(); return d; } Has in an ASP.Net appication that two codes difference? thanks Well for a start your second piece of code would return Task<T> rather than T . The ultimate answer is "it depends". If your page needs to access multiple data

How do I convert this to an async task?

风流意气都作罢 提交于 2019-11-28 12:03:26
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 minute, lets pretend that Task.Delay() does not exist and I actually have to use Thread.Sleep(); I know

How to await a method in a Linq query

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 10:53:10
Trying to use the await keyword in a LINQ query and I get this: The 'await' operator may only be used in a query expression within the first collection expression of the initial 'from' clause or within the collection expression of a 'join' clause Sample Code: var data = (from id in ids let d = await LoadDataAsync(id) select d); Is it not possible to await something in a LINQ query, or does it need to be structured a different way? Stephen Cleary LINQ has very limited support for async / await . For LINQ-to-objects, the only really useful operation I know of is to do a Select with an async

C# async methods still hang UI

夙愿已清 提交于 2019-11-28 10:08:37
I have these two methods, that I want to run async to keep the UI responsive. However, it's still hanging the UI. Any suggestions? async void DoScrape() { var feed = new Feed(); var results = await feed.GetList(); foreach (var itemObject in results) { var item = new ListViewItem(itemObject.Title); item.SubItems.Add(itemObject.Link); item.SubItems.Add(itemObject.Description); LstResults.Items.Add(item); } } public class Feed { async public Task<List<ItemObject>> GetList() { var client = new WebClient(); string content = await client.DownloadStringTaskAsync(new Uri("anyUrl")); var lstItemObjects

How to yield from parallel tasks in .NET 4.5

心已入冬 提交于 2019-11-28 10:07:46
I would like to use .NET iterator with parallel Tasks/await?. Something like this: IEnumerable<TDst> Foo<TSrc, TDest>(IEnumerable<TSrc> source) { Parallel.ForEach( source, s=> { // Ordering is NOT important // items can be yielded as soon as they are done yield return ExecuteOrDownloadSomething(s); } } Unfortunately .NET cannot natively handle this. Best answer so far by @svick - use AsParallel(). BONUS: Any simple async/await code that implements multiple publishers and a single subscriber? The subscriber would yield, and the pubs would process. (core libraries only) This seems like a job for

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

半腔热情 提交于 2019-11-28 09:07:17
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 WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov")); } } As far as I understand my

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

大憨熊 提交于 2019-11-28 08:28:07
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(); } } So given a Metro UI with two buttons and their click event handlers, the only difference is that the