c#-5.0

Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process

╄→尐↘猪︶ㄣ 提交于 2019-11-27 11:09:51
问题 I have a project that the framework is targeting .NET Framework 4.6.1 , as part of the continuous integration process on the tfs we created a Build Solution task to ensure that the code compiles correctly. Now the TFS server has the latest version of the .Net Famework 4.6.2 . On the register this is the value for the Release key of the framework On all other OS versions: 394806 => .NET Framework 4.6.2 But when the build runs it comes with this error: Error CS1056: Unexpected character '$' I

multiple parallel async calls with await

和自甴很熟 提交于 2019-11-27 10:29:43
问题 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

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

核能气质少年 提交于 2019-11-27 10:29:19
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 only way to accomplish this? And is there a way to wrap the call to GetStringFromUrl(url,callback) in the

How to implement INotifyPropertyChanged in C# 6.0?

拥有回忆 提交于 2019-11-27 08:52:19
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 this construction is used? After incorporating the various changes, the code will look like this. I've

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

蹲街弑〆低调 提交于 2019-11-27 08:50:46
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? Jon Skeet 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 . In theory one could use older tools from VS2010 along with the older CTP library, I'd strongly

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

不羁的心 提交于 2019-11-27 08:05:42
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 solution. What is the best-practice here? I thought of using a custom awaiter but the built-in

Task.Yield - real usages?

五迷三道 提交于 2019-11-27 07:33:51
I've been reading about Task.Yield , And as a Javascript developer I can tell that's it's job is exactly the same as setTimeout(function (){...},0); in terms of letting the main single thread deal with other stuff aka : "don't take all the power , release from time time - so others would have some too..." In js it's working particular in long loops. ( don't make the browser freeze... ) But I saw this example here : public static async Task < int > FindSeriesSum(int i1) { int sum = 0; for (int i = 0; i < i1; i++) { sum += i; if (i % 1000 == 0) ( after a bulk , release power to main thread)

Request.Content.ReadAsMultipartAsync never returns

让人想犯罪 __ 提交于 2019-11-27 06:33:14
I have an API for a system written using the ASP.NET Web Api and am trying to extend it to allow images to be uploaded. I have done some googling and found how the recommended way to accept files using MultpartMemoryStreamProvider and some async methods but my await on the ReadAsMultipartAsync never returns. Here is the code: [HttpPost] public async Task<HttpResponseMessage> LowResImage(int id) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartMemoryStreamProvider(); try { await Request.Content

How does Task<int> become an int?

主宰稳场 提交于 2019-11-27 05:57:37
We have this method: async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); // You can do work here that doesn't rely on the string from GetStringAsync. DoIndependentWork(); string urlContents = await getStringTask; //The thing is that this returns an int to a method that has a return type of Task<int> return urlContents.Length; } Does an implicit conversion occur between Task<int> and int ? If not, then what is happening? How is it implemented to work? Jon Skeet Does an implicit conversion

How do you create an asynchronous method in C#?

北城余情 提交于 2019-11-27 05:47:54
Every blog post I've read tells you how to consume an asynchronous method in C#, but for some odd reason never explain how to build your own asynchronous methods to consume. So I have this code right now that consumes my method: private async void button1_Click(object sender, EventArgs e) { var now = await CountToAsync(1000); label1.Text = now.ToString(); } And I wrote this method that is CountToAsync : private Task<DateTime> CountToAsync(int num = 1000) { return Task.Factory.StartNew(() => { for (int i = 0; i < num; i++) { Console.WriteLine("#{0}", i); } }).ContinueWith(x => DateTime.Now); }