I have a small request.
I admit that I did not use multithreading much earlier .Net 4.5, but with the new async/await functionality I decided to give it a try. I started experimenting with him, and everything seems fine, but I could not find a solution to my "problem" anywhere on the Internet.
So everyone explains how await can be used with the newly formed .NET platform methods. (e.g. WriteAsync() , ReadAsync() , etc.), but what if I wanted to use it for my own methods? For example, let's say that I perform an extremely expensive calculation and want all my 4 cores to work on it. I would have something like this:
async Task DoLotsOfWork<T>(T[] data, int start, int end) {
But since I do not have the await keyword, the method is simply considered synchronous. I would like to call it 4 times outside so that it can work on all of my cores, while I show something reasonable to the user (for example, "Please wait ..."). The only solution I could figure out was to add await Task.Yield(); at the beginning of the method. Something like that:
async Task DoLotsOfWork<T>(T[] data, int start, int end) { await Task.Yield();
In this case, the method will behave as I expected. But is there a better solution for this? I feel this should be simpler / smarter than writing this particular line of code. I understand that I can create a Task/Thread object and call the Start() method, but this requires even more work. I just thought that with the new async / await function it would be easier.
source share