c#-5.0

Why can't “async void” unit tests be recognized?

房东的猫 提交于 2019-11-26 22:46:01
问题 async void unit tests cannot be run within Visual Studio 2012: [TestClass] public class MyTestClass { [TestMethod] public async void InvisibleMyTestMethod() { await Task.Delay(1000); Assert.IsTrue(true); } } If I want to have an asynchronous unit test, the test method has to return a Task: [TestMethod] public async Task VisibleMyTestMethod() { await Task.Delay(1000); Assert.IsTrue(true); } Why is it so? Not that I absolutely need to have an async void test method, I am just curious. Visual

Await in catch block

ぃ、小莉子 提交于 2019-11-26 22:31:16
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's forbidden for whatever reason but what's the correct code pattern here? EDIT: The good news is that C# 6

A good solution for await in try/catch/finally?

狂风中的少年 提交于 2019-11-26 22:30:40
问题 I need to call an async method in a catch block before throwing again the exception (with its stack trace) like this : try { // Do something } catch { // <- Clean things here with async methods throw; } But unfortunately you can't use await in a catch or finally block. I learned it's because the compiler doesn't have any way to go back in a catch block to execute what is after your await instruction or something like that... I tried to use Task.Wait() to replace await and I got a deadlock. I

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

点点圈 提交于 2019-11-26 21:56:23
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? In short NO From Asynchronous Programming with Async and Await : Threads The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't

Async Implementation of IValueConverter

白昼怎懂夜的黑 提交于 2019-11-26 20:13:54
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"); } } You probably don't want to call Task.Result , for a couple of reasons. Firstly, as I explain in detail on my blog, you can deadlock unless your

What actually happens when using async/await inside a LINQ statement?

瘦欲@ 提交于 2019-11-26 19:58:42
问题 The following snippet compiles, but I'd expect it to await the task result instead of giving me a List<Task<T>> . var foo = bars.Select(async bar => await Baz(bar)).ToList() As pointed out here, you need to use Task.WhenAll : var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList(); await Task.WhenAll(tasks); But a comment points out that the async and await inside the Select() is not needed: var tasks = foos.Select(foo => DoSomethingAsync(foo)).ToList(); A similar question

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

本秂侑毒 提交于 2019-11-26 19:57:35
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 finalResult; } However, I get a compiler error citing "unable to load message string from resources". Here is

How do you implement an async action delegate method?

。_饼干妹妹 提交于 2019-11-26 19:13:08
问题 A little background information. I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes. Different methods however, would produce different results and error codes but the result object would generally be instantiated the same way. To save some time and also to learn more about async/await capabilities in C#, I am trying to wrap all the method bodies of my web api actions in an asynchronous action

How do I disable C# 6 Support in Visual Studio 2015?

空扰寡人 提交于 2019-11-26 18:52:50
问题 Background We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6. We have no intention to use C# 6 within this particular solution (as much as I'd like to). Problem Visual Studio and ReSharper suggest helpful C# 6 language constructs that render the solution inoperable in earlier versions of Visual Studio without C#6 support. I've disabled the ReSharper C#6 support but I can't seem to disable / limit C#

How to Async Files.ReadAllLines and await for results?

只愿长相守 提交于 2019-11-26 18:08:16
问题 I have the following code, private void button1_Click(object sender, RoutedEventArgs e) { button1.IsEnabled = false; var s = File.ReadAllLines("Words.txt").ToList(); // my WPF app hangs here // do something with s button1.IsEnabled = true; } Words.txt has a ton of words which i read into the s variable, I am trying to make use of async and await keywords in C# 5 using Async CTP Library so the WPF app doesn't hang. So far I have the following code, private async void button1_Click(object