C# Task WaitAll - allow another cycle only when all previous tasks finished ?
Hi,
I have a button where I run 4 tasks asynchronously, result is some string based on looping through many directories on my FileSystem. If I start clicking on a button continuously everything get's messed up, because some previous taks are still running in parallel. Here is my code :
Code:
var tests = new[]
{
Task.Factory.StartNew(() => Test1()),
Task.Factory.StartNew(() => Test2()),
Task.Factory.StartNew(() => Test3()),
Task.Factory.StartNew(() => Test4()),
};
Task.WaitAll(tests);
From what I read I could use Task.WhenAll, but I can't manage It correctly. Can somebody show me how should It look like ?
Re: C# Task WaitAll - allow another cycle only when all previous tasks finished ?
If that code is in the button click it won't stop you clicking the button and creating another four tasks. Could you not disable the button once it has been clicked and re-enable it after the event completes?
Re: C# Task WaitAll - allow another cycle only when all previous tasks finished ?
Hi PlausiblyDamp,
this is what I tried in first place, but doesn't have effect. Task.WaitAll doesn't actually wait for all threads to finish, so code goes forward with threads still running. And Tast.WhenAll just produces another task when others are all finished, but I can't get desired results from finished ones.
Re: C# Task WaitAll - allow another cycle only when all previous tasks finished ?
There isn't much point in having the background threads if you're hang the GUI until they're done.
But, I haven't worked with Tasks so can't advise you. I just create my own background threads when I need to.
Re: C# Task WaitAll - allow another cycle only when all previous tasks finished ?
Quote:
I just create my own background threads when I need to.
You mean BackGroundworker ? ....That is something I'm trying to avoid. Don't know much difference between both techniques, but I've read something about BackGroundWorker being kind of obsolete. And I want to learn something new, so I'm trying with Tasks this time. Code also looks a bit nicer, but there are so many things to be careful about so It's not really easy.