|
-
Feb 27th, 2018, 08:37 AM
#1
Thread Starter
Fanatic Member
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 ?
-
Feb 27th, 2018, 11:18 AM
#2
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?
-
Feb 28th, 2018, 01:10 AM
#3
Thread Starter
Fanatic Member
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.
-
Feb 28th, 2018, 06:17 AM
#4
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.
Last edited by passel; Feb 28th, 2018 at 06:21 AM.
-
Feb 28th, 2018, 07:27 AM
#5
Thread Starter
Fanatic Member
Re: C# Task WaitAll - allow another cycle only when all previous tasks finished ?
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|