c#-5.0

Do the new C# 5.0 'async' and 'await' keywords use multiple cores?

爷,独闯天下 提交于 2019-11-26 08:47:57
问题 Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the calling thread. My question is, do these methods actually take advantage of multiple cores and run in parallel or does the async method run in the same thread core as the caller? 回答1: Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the

Await in catch block

十年热恋 提交于 2019-11-26 08:24:30
问题 I have the following code: WebClient wc = new WebClient(); string result; try { result = await wc.DownloadStringTaskAsync( new Uri( \"http://badurl\" ) ); } catch { result = await wc.DownloadStringTaskAsync( new Uri( \"http://fallbackurl\" ) ); } Basically I want to download from a URL and when it fails with an exception I want to download from another URL. Both time async of course. However the code does not compile, because of error CS1985: Cannot await in the body of a catch clause OK, it\

Does the use of async/await create a new thread?

a 夏天 提交于 2019-11-26 08:06:21
问题 I am new to TPL and I am wondering: How does the asynchronous programming support that is new to C# 5.0 (via the new async and await keywords) relate to the creation of threads? Specifically, does the use of async/await create a new thread each time that they are used? And if there many nested methods that use async/await , is a new thread created for each of those methods? 回答1: In short NO From Asynchronous Programming with Async and Await : Threads The async and await keywords don't cause

Async Implementation of IValueConverter

烂漫一生 提交于 2019-11-26 07:29:39
问题 If an async Method which I want to trigger inside a IValueConverter. Is there a better Wait then forcing it to be synchronous by calling the result Property? public async Task<object> Convert(object value, Type targetType, object parameter, string language) { StorageFile file = value as StorageFile; if (file != null) { var image = ImageEx.ImageFromFile(file).Result; return image; } else { throw new InvalidOperationException(\"invalid parameter\"); } } 回答1: You probably don't want to call Task

Is it possible to “await yield return DoSomethingAsync()”

懵懂的女人 提交于 2019-11-26 07:28:54
问题 Are regular iterator blocks (i.e. \"yield return\") incompatible with \"async\" and \"await\"? This gives a good idea of what I\'m trying to do: async Task<IEnumerable<Foo>> Method(String [] Strs) { // I want to compose the single result to the final result, so I use the SelectMany var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url\'s html code ); return

async - stay on the current thread?

我们两清 提交于 2019-11-26 04:27:03
问题 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.

Has foreach&#39;s use of variables been changed in C# 5?

限于喜欢 提交于 2019-11-26 03:38:01
问题 In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that \"FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers\" with regards to how the foreach loops uses the variable. In the next version each time you run through the \"foreach\" loop we will generate a new loop variable rather than closing over the same variable every time. This is a \"breaking\" change but in the vast majority of cases the \"break\" will be fixing

Fire-and-forget with async vs “old async delegate”

倾然丶 夕夏残阳落幕 提交于 2019-11-26 03:24:36
问题 I am trying to replace my old fire-and-forget calls with a new syntax, hoping for more simplicity and it seems to be eluding me. Here\'s an example class Program { static void DoIt(string entry) { Console.WriteLine(\"Message: \" + entry); } static async void DoIt2(string entry) { await Task.Yield(); Console.WriteLine(\"Message2: \" + entry); } static void Main(string[] args) { // old way Action<string> async = DoIt; async.BeginInvoke(\"Test\", ar => { async.EndInvoke(ar); ar.AsyncWaitHandle

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

亡梦爱人 提交于 2019-11-26 03:00:14
问题 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

Why does this async action hang?

淺唱寂寞╮ 提交于 2019-11-26 02:30:07
问题 I have a multi-tier .Net 4.5 application calling a method using C#\'s new async and await keywords that just hangs and I can\'t see why. At the bottom I have an async method that extents our database utility OurDBConn (basically a wrapper for the underlying DBConnection and DBCommand objects): public static async Task<T> ExecuteAsync<T>(this OurDBConn dataSource, Func<OurDBConn, T> function) { string connectionString = dataSource.ConnectionString; // Start the SQL and pass back to the caller