Results 1 to 5 of 5

Thread: C# Task WaitAll - allow another cycle only when all previous tasks finished ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    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 ?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    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?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    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.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    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
  •  



Click Here to Expand Forum to Full Width