c#-5.0

Unnecessary async/await when await is last?

本秂侑毒 提交于 2019-11-30 08:54:31
问题 I've been dealing quite a lot with async await lately (read every possible article including Stephen's and Jon's last 2 chapters) , but I have come to conclusion and I don't know if it's 100% correct. - hence my question . Since async only allows the word await to be present , i'll leave async aside. AFAIU , await is all about continuation . instead of writing functional (continuational) code , write synchronous code. ( i like to refer it as callback'able code) So when the compiler reaches

Limit number of Threads in Task Parallel Library

霸气de小男生 提交于 2019-11-30 07:26:21
I have few hundreds of files i need to upload to Azure Blob Storage. I want to use parallel task library. But instead of running all the 100 threads to upload in a foreach on list of files, how can i put a limit on max number of threads that it can use and finish the job in parallel. or does it balance the things automatically? You should not be using threads for this at all. There's a Task -based API for this, which is naturally asynchronous: CloudBlockBlob.UploadFromFileAsync . Use it with async/await and SemaphoreSlim to throttle the number of parallel uploads. Example (untested): const MAX

Determining the caller inside a setter — or setting properties, silently

随声附和 提交于 2019-11-30 06:56:37
Given a standard view model implementation, when a property changes, is there any way to determine the originator of the change? In other words, in the following view model, I would like the "sender" argument of the "PropertyChanged" event to be the actual object that called the Prop1 setter: public class ViewModel : INotifyPropertyChanged { public double Prop1 { get { return _prop1; } set { if (_prop1 == value) return; _prop1 = value; // here, can I determine the sender? RaisePropertyChanged(propertyName: "Prop1", sender: this); } } private double _prop1; // TODO implement

Can I have my assembly reference any version of another assembly? [duplicate]

一笑奈何 提交于 2019-11-30 06:52:47
This question already has an answer here: Is it possible to replace a reference to a strongly-named assembly with a “weak” reference? 3 answers I am developing a class library ( MyClassLibrary ). I depend on a third party class library ( ThirdPartyClassLibrary ). I need to use the same version of ThirdPartyClassLibrary as my users. e.g., if I set a static value in ThirdPartyClassLibrary the user needs to see that change. Users of my class may be depending on any one of 4 different versions of ThirdPartyClassLibrary . ThirdPartyClassLibrary is large, I do not want to distribute it with my

await Task.WhenAll() vs Task.WhenAll().Wait()

最后都变了- 提交于 2019-11-30 06:05:33
I have a method that produces an array of tasks ( See my previous post about threading ) and at the end of this method I have the following options: await Task.WhenAll(tasks); // done in a method marked with async Task.WhenAll(tasks).Wait(); // done in any type of method Task.WaitAll(tasks); Basically I am wanting to know what the difference between the two whenall s are as the first one doesn't seem to wait until tasks are completed where as the second one does, but I'm not wanting to use the second one if it's not asynchronus. I have included the third option as I understand that this will

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

这一生的挚爱 提交于 2019-11-30 06:01:19
问题 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 回答1: Well for a start your second piece of code would return

Await alternative in .NET 4.0?

一曲冷凌霜 提交于 2019-11-30 03:23:12
问题 What would be the best alternative for the await keyword in .NET 4.0 ? I have a method which needs to return a value after an asynchronous operation. I noticed the wait() method blocks the thread completely thus rendering the asynchronous operation useless. What are my options to run the async operation while still freeing the UI thread ? 回答1: I think your basic options are Using Task and .ContinueWith() Using the Async CTP and async / await Using Reactive Extensions The easiest way is

Convert async lambda expression to delegate type System.Func<T>?

偶尔善良 提交于 2019-11-30 03:02:28
I have an async method inside a portable class library with this signature: private async Task<T> _Fetch<T>(Uri uri) It fetches a resource that is cast back as a concrete type T. I'm working with a 3rd party cache library ( Akavache ) that requires a Func<T> as one of the parameters and have tried to do so in this manner: await this.CacheProvider.GetOrCreateObject<T>(key, async () => await _Fetch<T>(uri), cacheExpiry); This results in the error: Cannot convert async lambda expression to delegate type ' System.Func<T> '. An async lambda expression may return void , Task or Task<T> , none of

Why return type of async must be void, Task or Task<T>

馋奶兔 提交于 2019-11-30 01:23:16
问题 I am trying get my hands dirty with async CTP and I noticed that the compiler complains about the async return type. What is the problem with other types? A simple demo static void Main(string[] args) { DoWork(); Console.WriteLine("Returned to main"); Console.Read(); } // why do I need to return void, Task or Task<T> here? // I know I can use something like Task<IEnumerable<string>> private static async string[] DoWork() { Console.WriteLine("DoWork started"); return await Task.Factory

Why does cancellation block for so long when cancelling a lot of HTTP requests?

耗尽温柔 提交于 2019-11-29 22:58:15
问题 Background I have some code that performs batch HTML page processing using content from one specific host. It tries to make a large number (~400) of simultaneous HTTP requests using HttpClient . I believe that the maximum number of simultaneous connections is restricted by ServicePointManager.DefaultConnectionLimit , so I'm not applying my own concurrency restrictions. After sending all of the requests asynchronously to HttpClient using Task.WhenAll , the entire batch operation can be