Does Task.Run guarantee each method will be run to completion










0















There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.



 List<Widget> widgetList = new List<Widget>() 

file list with 100 widget objects

List<Task> tasks = new List<Task>();

foreach (Widget widget in widgetList)

Task t = Task.Run(async () =>

var result = await widget.Analyze(newData);
ReportResults(result);
);
tasks.Add(t);

Task.WaitAll(tasks.ToArray());


No it is not a complete code example but should be enough for the question which is as follows:
Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?



TIA,
Doug










share|improve this question


























    0















    There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.



     List<Widget> widgetList = new List<Widget>() 

    file list with 100 widget objects

    List<Task> tasks = new List<Task>();

    foreach (Widget widget in widgetList)

    Task t = Task.Run(async () =>

    var result = await widget.Analyze(newData);
    ReportResults(result);
    );
    tasks.Add(t);

    Task.WaitAll(tasks.ToArray());


    No it is not a complete code example but should be enough for the question which is as follows:
    Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?



    TIA,
    Doug










    share|improve this question
























      0












      0








      0








      There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.



       List<Widget> widgetList = new List<Widget>() 

      file list with 100 widget objects

      List<Task> tasks = new List<Task>();

      foreach (Widget widget in widgetList)

      Task t = Task.Run(async () =>

      var result = await widget.Analyze(newData);
      ReportResults(result);
      );
      tasks.Add(t);

      Task.WaitAll(tasks.ToArray());


      No it is not a complete code example but should be enough for the question which is as follows:
      Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?



      TIA,
      Doug










      share|improve this question














      There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.



       List<Widget> widgetList = new List<Widget>() 

      file list with 100 widget objects

      List<Task> tasks = new List<Task>();

      foreach (Widget widget in widgetList)

      Task t = Task.Run(async () =>

      var result = await widget.Analyze(newData);
      ReportResults(result);
      );
      tasks.Add(t);

      Task.WaitAll(tasks.ToArray());


      No it is not a complete code example but should be enough for the question which is as follows:
      Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?



      TIA,
      Doug







      c#-4.0 async-await task-parallel-library






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 22:38









      AeroClassicsAeroClassics

      619414




      619414






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Each method will run to completion, unless something tears down the process or application domain.



          As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults works.



          I would do something like this instead:



          var results = await Task.WhenAll(
          for widget in widgetList
          select widget.Analyze(newData));

          foreach (var result in results)

          ReportResults(result);



          Note: async-await is a C# 5.0 feature, not available in C# 4.0.






          share|improve this answer























          • Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

            – AeroClassics
            Nov 16 '18 at 19:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309787%2fdoes-task-run-guarantee-each-method-will-be-run-to-completion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Each method will run to completion, unless something tears down the process or application domain.



          As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults works.



          I would do something like this instead:



          var results = await Task.WhenAll(
          for widget in widgetList
          select widget.Analyze(newData));

          foreach (var result in results)

          ReportResults(result);



          Note: async-await is a C# 5.0 feature, not available in C# 4.0.






          share|improve this answer























          • Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

            – AeroClassics
            Nov 16 '18 at 19:29















          1














          Each method will run to completion, unless something tears down the process or application domain.



          As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults works.



          I would do something like this instead:



          var results = await Task.WhenAll(
          for widget in widgetList
          select widget.Analyze(newData));

          foreach (var result in results)

          ReportResults(result);



          Note: async-await is a C# 5.0 feature, not available in C# 4.0.






          share|improve this answer























          • Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

            – AeroClassics
            Nov 16 '18 at 19:29













          1












          1








          1







          Each method will run to completion, unless something tears down the process or application domain.



          As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults works.



          I would do something like this instead:



          var results = await Task.WhenAll(
          for widget in widgetList
          select widget.Analyze(newData));

          foreach (var result in results)

          ReportResults(result);



          Note: async-await is a C# 5.0 feature, not available in C# 4.0.






          share|improve this answer













          Each method will run to completion, unless something tears down the process or application domain.



          As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults works.



          I would do something like this instead:



          var results = await Task.WhenAll(
          for widget in widgetList
          select widget.Analyze(newData));

          foreach (var result in results)

          ReportResults(result);



          Note: async-await is a C# 5.0 feature, not available in C# 4.0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 0:33









          Paulo MorgadoPaulo Morgado

          6,13811533




          6,13811533












          • Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

            – AeroClassics
            Nov 16 '18 at 19:29

















          • Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

            – AeroClassics
            Nov 16 '18 at 19:29
















          Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

          – AeroClassics
          Nov 16 '18 at 19:29





          Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.

          – AeroClassics
          Nov 16 '18 at 19:29



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309787%2fdoes-task-run-guarantee-each-method-will-be-run-to-completion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo