Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. [Solved] Run application until Tasks are finished?

[Solved] Run application until Tasks are finished?

Scheduled Pinned Locked Moved C#
helpdata-structuresxmlquestion
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dralken
    wrote on last edited by
    #1

    I'm writing an application that performs a few independant tasks in the background. It's a console application. My issue is that when the program is run, it starts to execute some of the stuff on the threads (processes maybe 1-2 lines of code) then quits before all the tasks are finished (I'm using the Task library, rather than standard Threads) Rather than cramming all of my code into the Main() function, I'm separating them out into different functions for readability. If I add a Console.ReadKey() at the end of the Main function, all the tasks run properly (they only take maybe 1-2 seconds, though over time that could change to several minutes, as it's performing file operations) and close properly when I hit a button. However, once I'm finished with it, I don't want to have make the user do any inputs (it's going to be a "scheduled" task), in fact the window won't even be shown. Part of my problem is that, by the nature of my program, I can't ever be completely sure of how many tasks are going to be running. The program pulls in a list of what needs to be done from an xml file. Is there a safe way to perhaps create a list of tasks, then at the end of the Main() function, have it wait till all those tasks are complete before the program closes? I tried using a

    List

    but it didn't seem very safe, as I'd be building the array with other functions then doing a foreach() on the list and starting each task.

    D 1 Reply Last reply
    0
    • D Dralken

      I'm writing an application that performs a few independant tasks in the background. It's a console application. My issue is that when the program is run, it starts to execute some of the stuff on the threads (processes maybe 1-2 lines of code) then quits before all the tasks are finished (I'm using the Task library, rather than standard Threads) Rather than cramming all of my code into the Main() function, I'm separating them out into different functions for readability. If I add a Console.ReadKey() at the end of the Main function, all the tasks run properly (they only take maybe 1-2 seconds, though over time that could change to several minutes, as it's performing file operations) and close properly when I hit a button. However, once I'm finished with it, I don't want to have make the user do any inputs (it's going to be a "scheduled" task), in fact the window won't even be shown. Part of my problem is that, by the nature of my program, I can't ever be completely sure of how many tasks are going to be running. The program pulls in a list of what needs to be done from an xml file. Is there a safe way to perhaps create a list of tasks, then at the end of the Main() function, have it wait till all those tasks are complete before the program closes? I tried using a

      List

      but it didn't seem very safe, as I'd be building the array with other functions then doing a foreach() on the list and starting each task.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Sorry, but I'm not downloading anything from an unknown URL. But, If you're indeed using Tasks, there is always Task.WaitAll()[^] which is a blocking call that waits for all of the specified Tasks to complete before proceeding.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Sorry, but I'm not downloading anything from an unknown URL. But, If you're indeed using Tasks, there is always Task.WaitAll()[^] which is a blocking call that waits for all of the specified Tasks to complete before proceeding.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        D Offline
        D Offline
        Dralken
        wrote on last edited by
        #3

        Problem with Task.WaitAll() is that it expects a fixed array (Task[]) of tasks, whereas the array could be anything from a single task to a dozen of them, and it's not known at run time. The tasks are also created and start in a separate function from the Main() function. I suppose I could (and might end up needing to) cram everything into Main() but I'm trying to avoid that as much as possible. (Also, it's Pastebin, you're not downloading anything [besides overall web cache of the website], it's just a copy-paste of my code with syntax highlighting.)

        D 1 Reply Last reply
        0
        • D Dralken

          Problem with Task.WaitAll() is that it expects a fixed array (Task[]) of tasks, whereas the array could be anything from a single task to a dozen of them, and it's not known at run time. The tasks are also created and start in a separate function from the Main() function. I suppose I could (and might end up needing to) cram everything into Main() but I'm trying to avoid that as much as possible. (Also, it's Pastebin, you're not downloading anything [besides overall web cache of the website], it's just a copy-paste of my code with syntax highlighting.)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Vouksh wrote:

          Problem with Task.WaitAll() is that it expects a fixed array (Task[]) of tasks, whereas the array could be anything from a single task to a dozen of them, and it's not known at run time.

          Bullshit. Create a List and add your Tasks to it. When you're done, call .ToArray() on the List and use you use that in Task.WaitAll. Task.WaitAll can go into your code that creates your tasks. Since it's a blocking call, control will NOT return back to Main to end the application before the tasks are complete.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Vouksh wrote:

            Problem with Task.WaitAll() is that it expects a fixed array (Task[]) of tasks, whereas the array could be anything from a single task to a dozen of them, and it's not known at run time.

            Bullshit. Create a List and add your Tasks to it. When you're done, call .ToArray() on the List and use you use that in Task.WaitAll. Task.WaitAll can go into your code that creates your tasks. Since it's a blocking call, control will NOT return back to Main to end the application before the tasks are complete.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            D Offline
            D Offline
            Dralken
            wrote on last edited by
            #5

            Thank you, I hadn't thought of using .ToArray() on a List. Forgot I could, honestly. I'll give it a try and, if it works, I'll mark this as solved. Edit: Worked perfectly! Thank you very much!

            B 1 Reply Last reply
            0
            • D Dralken

              Thank you, I hadn't thought of using .ToArray() on a List. Forgot I could, honestly. I'll give it a try and, if it works, I'll mark this as solved. Edit: Worked perfectly! Thank you very much!

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              Dave K. has answered your question, but I think you might get some benefit from a fantastic article by Keith Barrow (revised Aug. 30, 2013), "Await Tasks in C#4 using Iterators," about creating the functionality of C#5.0's await/async functionality for people using C#4.0. It's one of the best written articles I have ever seen on CodeProject, and it contains a wealth of information that will give you "deep background" on threads, and asynchronous events [^]. good luck, Bill

              Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

              D K 2 Replies Last reply
              0
              • B BillWoodruff

                Dave K. has answered your question, but I think you might get some benefit from a fantastic article by Keith Barrow (revised Aug. 30, 2013), "Await Tasks in C#4 using Iterators," about creating the functionality of C#5.0's await/async functionality for people using C#4.0. It's one of the best written articles I have ever seen on CodeProject, and it contains a wealth of information that will give you "deep background" on threads, and asynchronous events [^]. good luck, Bill

                Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                D Offline
                D Offline
                Dralken
                wrote on last edited by
                #7

                Thanks, I'll check it out.

                1 Reply Last reply
                0
                • B BillWoodruff

                  Dave K. has answered your question, but I think you might get some benefit from a fantastic article by Keith Barrow (revised Aug. 30, 2013), "Await Tasks in C#4 using Iterators," about creating the functionality of C#5.0's await/async functionality for people using C#4.0. It's one of the best written articles I have ever seen on CodeProject, and it contains a wealth of information that will give you "deep background" on threads, and asynchronous events [^]. good luck, Bill

                  Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                  K Offline
                  K Offline
                  Keith L Robertson
                  wrote on last edited by
                  #8

                  BillWoodruff wrote: article by Keith Barrow (revised Aug. 30, 2013), "Await Tasks in C#4 using Iterators," Thanks for the very kind words :-\ and article "plug" (promotion), my friend! The author is a different "Keith", though. :java:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups