![]() Async should usually be prefered if possible as it uses your resources more effectively. P.S.: Your functions need to be async at their core, otherwise you need to use something like Parallel.ForEach. Pagico comes with a built-in feature called Personal Cloud, which lets you sync all data among your computers and mobile devices (iOS or Android). I reduced the delay and added an additional output there.Īlternatively you could use a shared concurrentbag to add the results inside the tasks (adding results to lists in seperate threads might cause weird issues like null entries), but I see no good reason for this in your usecase. Here is a slightly modified fiddle to try it out online. You need to change your pattern here - if you have many long running tasks and want to run them asynchronously you should initiate those tasks seperately and collect/transform the results afterwards: static async Task Main(string args) No, you cannot add to a list asynchronously. Want to collaborate with teammates Create a Team workspace and selectively sync data with your colleagues. using System Ĭonsole.WriteLine("ran tasks in " + (EndTime - StartTime).TotalSeconds + " seconds") Pagico comes with a built-in feature called Personal Cloud, which lets you sync all data among your computers and mobile devices (iOS or Android). I took the async/await out of Task1 and Task2 and just created a Task and passed Task1 and Task2 in as parameters. ![]() I modified Compufreak's code slightly to a way that makes more sense to me and I'm wondering if there is any actual difference in the way the code works. I have a related question although I'm not sure if this is how I'm supposed to ask it. How would I do it asynchronously? Is it even possible? Would you get "collisions" if you tried to add to the list at the same time? using System.Threading Ĭompufreak's answer was just what I was looking for. Here's a simple example of adding 2 items to a list synchronously. The method to add the item can be asynchronous so I'm wondering if I can add all of them using async/await somehow. There's about 50 items in the list and it can take up to 20 seconds to add each item. I'm creating a list in C# synchronously but it's slow.
0 Comments
Leave a Reply. |