Passing instance property to static method in Async Tasks










1















The code bellow:



namespace ConsoleApp2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program

static async Task Main(string args)

var split = new SplitService();

var tasks = new List<Task>();
for (var nI = 0; nI < 10; nI++)

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;

var task = new Task (() => split.Split(fathers));
tasks.Add(task);
;

foreach (var task in tasks)

task.Start();


Console.ReadKey();



public class SplitService

public IEnumerable<Father> Split(List<Father> fathers)

this.FatherProperties = fathers.GetFatherValues();

this.RecalculateProperties(fathers);

return fathers;


public List<FatherProperties> FatherProperties get; private set; = new List<FatherProperties>();

public void RecalculateProperties(List<Father> fathers)

fathers.Update(this.FatherProperties);



public static class FatherExtensions

public static List<FatherProperties> GetFatherValues(this List<Father> fathers)

return new List<FatherProperties>

new FatherProperties

FatherId = fathers.FirstOrDefault().Id

;


public static void Update(this List<Father> fathers, List<FatherProperties> properties)

foreach (var father in fathers)

var match =
(
from value in properties
where value.FatherId == father.Id
select new

father.Id
).SingleOrDefault();

if (match == null)

Console.WriteLine("Error");

else

Console.WriteLine(match.Id);





public class Father

public Guid Id get; set;


public class FatherProperties

public Guid FatherId get; set;




Gives errors is more than one thread is running.
Can anyone help me with the reason why?
Is this line:



fathers.Update(this.FatherProperties);


changes to this:



fathers.Update(father.GetFatherValues());


The code works.



Is this related to the access of the property? I can't figure out why.
I tried to read many websites but still can't find the reason of this error.



Thanks for the help.










share|improve this question






















  • Gives errors is more than one thread is running. What line of code throws what exception?

    – mjwills
    Nov 13 '18 at 9:06











  • There is no exception. The code should always have a match. If no match is thrown than I have an error.

    – user1425879
    Nov 13 '18 at 9:08











  • There's no exception. Maybe the name is not the best.

    – user1425879
    Nov 13 '18 at 9:12











  • What does Gives errors is more than one thread is running. mean?

    – mjwills
    Nov 13 '18 at 9:12











  • If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

    – user1425879
    Nov 13 '18 at 9:13















1















The code bellow:



namespace ConsoleApp2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program

static async Task Main(string args)

var split = new SplitService();

var tasks = new List<Task>();
for (var nI = 0; nI < 10; nI++)

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;

var task = new Task (() => split.Split(fathers));
tasks.Add(task);
;

foreach (var task in tasks)

task.Start();


Console.ReadKey();



public class SplitService

public IEnumerable<Father> Split(List<Father> fathers)

this.FatherProperties = fathers.GetFatherValues();

this.RecalculateProperties(fathers);

return fathers;


public List<FatherProperties> FatherProperties get; private set; = new List<FatherProperties>();

public void RecalculateProperties(List<Father> fathers)

fathers.Update(this.FatherProperties);



public static class FatherExtensions

public static List<FatherProperties> GetFatherValues(this List<Father> fathers)

return new List<FatherProperties>

new FatherProperties

FatherId = fathers.FirstOrDefault().Id

;


public static void Update(this List<Father> fathers, List<FatherProperties> properties)

foreach (var father in fathers)

var match =
(
from value in properties
where value.FatherId == father.Id
select new

father.Id
).SingleOrDefault();

if (match == null)

Console.WriteLine("Error");

else

Console.WriteLine(match.Id);





public class Father

public Guid Id get; set;


public class FatherProperties

public Guid FatherId get; set;




Gives errors is more than one thread is running.
Can anyone help me with the reason why?
Is this line:



fathers.Update(this.FatherProperties);


changes to this:



fathers.Update(father.GetFatherValues());


The code works.



Is this related to the access of the property? I can't figure out why.
I tried to read many websites but still can't find the reason of this error.



Thanks for the help.










share|improve this question






















  • Gives errors is more than one thread is running. What line of code throws what exception?

    – mjwills
    Nov 13 '18 at 9:06











  • There is no exception. The code should always have a match. If no match is thrown than I have an error.

    – user1425879
    Nov 13 '18 at 9:08











  • There's no exception. Maybe the name is not the best.

    – user1425879
    Nov 13 '18 at 9:12











  • What does Gives errors is more than one thread is running. mean?

    – mjwills
    Nov 13 '18 at 9:12











  • If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

    – user1425879
    Nov 13 '18 at 9:13













1












1








1








The code bellow:



namespace ConsoleApp2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program

static async Task Main(string args)

var split = new SplitService();

var tasks = new List<Task>();
for (var nI = 0; nI < 10; nI++)

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;

var task = new Task (() => split.Split(fathers));
tasks.Add(task);
;

foreach (var task in tasks)

task.Start();


Console.ReadKey();



public class SplitService

public IEnumerable<Father> Split(List<Father> fathers)

this.FatherProperties = fathers.GetFatherValues();

this.RecalculateProperties(fathers);

return fathers;


public List<FatherProperties> FatherProperties get; private set; = new List<FatherProperties>();

public void RecalculateProperties(List<Father> fathers)

fathers.Update(this.FatherProperties);



public static class FatherExtensions

public static List<FatherProperties> GetFatherValues(this List<Father> fathers)

return new List<FatherProperties>

new FatherProperties

FatherId = fathers.FirstOrDefault().Id

;


public static void Update(this List<Father> fathers, List<FatherProperties> properties)

foreach (var father in fathers)

var match =
(
from value in properties
where value.FatherId == father.Id
select new

father.Id
).SingleOrDefault();

if (match == null)

Console.WriteLine("Error");

else

Console.WriteLine(match.Id);





public class Father

public Guid Id get; set;


public class FatherProperties

public Guid FatherId get; set;




Gives errors is more than one thread is running.
Can anyone help me with the reason why?
Is this line:



fathers.Update(this.FatherProperties);


changes to this:



fathers.Update(father.GetFatherValues());


The code works.



Is this related to the access of the property? I can't figure out why.
I tried to read many websites but still can't find the reason of this error.



Thanks for the help.










share|improve this question














The code bellow:



namespace ConsoleApp2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program

static async Task Main(string args)

var split = new SplitService();

var tasks = new List<Task>();
for (var nI = 0; nI < 10; nI++)

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;

var task = new Task (() => split.Split(fathers));
tasks.Add(task);
;

foreach (var task in tasks)

task.Start();


Console.ReadKey();



public class SplitService

public IEnumerable<Father> Split(List<Father> fathers)

this.FatherProperties = fathers.GetFatherValues();

this.RecalculateProperties(fathers);

return fathers;


public List<FatherProperties> FatherProperties get; private set; = new List<FatherProperties>();

public void RecalculateProperties(List<Father> fathers)

fathers.Update(this.FatherProperties);



public static class FatherExtensions

public static List<FatherProperties> GetFatherValues(this List<Father> fathers)

return new List<FatherProperties>

new FatherProperties

FatherId = fathers.FirstOrDefault().Id

;


public static void Update(this List<Father> fathers, List<FatherProperties> properties)

foreach (var father in fathers)

var match =
(
from value in properties
where value.FatherId == father.Id
select new

father.Id
).SingleOrDefault();

if (match == null)

Console.WriteLine("Error");

else

Console.WriteLine(match.Id);





public class Father

public Guid Id get; set;


public class FatherProperties

public Guid FatherId get; set;




Gives errors is more than one thread is running.
Can anyone help me with the reason why?
Is this line:



fathers.Update(this.FatherProperties);


changes to this:



fathers.Update(father.GetFatherValues());


The code works.



Is this related to the access of the property? I can't figure out why.
I tried to read many websites but still can't find the reason of this error.



Thanks for the help.







c# asynchronous static






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:05









user1425879user1425879

156




156












  • Gives errors is more than one thread is running. What line of code throws what exception?

    – mjwills
    Nov 13 '18 at 9:06











  • There is no exception. The code should always have a match. If no match is thrown than I have an error.

    – user1425879
    Nov 13 '18 at 9:08











  • There's no exception. Maybe the name is not the best.

    – user1425879
    Nov 13 '18 at 9:12











  • What does Gives errors is more than one thread is running. mean?

    – mjwills
    Nov 13 '18 at 9:12











  • If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

    – user1425879
    Nov 13 '18 at 9:13

















  • Gives errors is more than one thread is running. What line of code throws what exception?

    – mjwills
    Nov 13 '18 at 9:06











  • There is no exception. The code should always have a match. If no match is thrown than I have an error.

    – user1425879
    Nov 13 '18 at 9:08











  • There's no exception. Maybe the name is not the best.

    – user1425879
    Nov 13 '18 at 9:12











  • What does Gives errors is more than one thread is running. mean?

    – mjwills
    Nov 13 '18 at 9:12











  • If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

    – user1425879
    Nov 13 '18 at 9:13
















Gives errors is more than one thread is running. What line of code throws what exception?

– mjwills
Nov 13 '18 at 9:06





Gives errors is more than one thread is running. What line of code throws what exception?

– mjwills
Nov 13 '18 at 9:06













There is no exception. The code should always have a match. If no match is thrown than I have an error.

– user1425879
Nov 13 '18 at 9:08





There is no exception. The code should always have a match. If no match is thrown than I have an error.

– user1425879
Nov 13 '18 at 9:08













There's no exception. Maybe the name is not the best.

– user1425879
Nov 13 '18 at 9:12





There's no exception. Maybe the name is not the best.

– user1425879
Nov 13 '18 at 9:12













What does Gives errors is more than one thread is running. mean?

– mjwills
Nov 13 '18 at 9:12





What does Gives errors is more than one thread is running. mean?

– mjwills
Nov 13 '18 at 9:12













If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

– user1425879
Nov 13 '18 at 9:13





If I produce a new object based in another with the same identifier. When I compare those two objects I should have same Ids on both objects. That is not what's happening.

– user1425879
Nov 13 '18 at 9:13












2 Answers
2






active

oldest

votes


















1














You are using the same instance of SplitService for all your tasks.
Inside you are modifying it's member FatherProperties.



Obviously, that's why you have this behavior.



Create a service for each task:



for (var nI = 0; nI < 10; nI++)

var split = new SplitService(); // <-- a dedicated service for each

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
var task = new Task (() => split.Split(fathers));
tasks.Add(task);
;





share|improve this answer























  • First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

    – user1425879
    Nov 13 '18 at 10:42












  • Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

    – Ofir Winegarten
    Nov 13 '18 at 10:50











  • Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

    – user1425879
    Nov 13 '18 at 11:52


















0














First: Your excessive use of extension methods make the code very hard to read.

Second: You are pulling away the very collection you are using th compare. You use only ONE SplitService and its FatherProperties and you are changeing them in every call to split.Split(fathers). Since you are doing it multithreaded, you compare in one thread while another just puts a new fatherproperties collection in there. When using the GetFatherValues() again you are avoiding this behavior and therefore not getting an error.



Put the creation of the splitservice into the iteration.



static void Main(string args)

var tasks = new List<Task>();
for (var nI = 0; nI < 100; nI++)

var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
var split = new SplitService();
var task = new Task(() => split.Split(fathers));
tasks.Add(task);
;

foreach (var task in tasks)

task.Start();


Console.ReadKey();






share|improve this answer
























    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%2f53277323%2fpassing-instance-property-to-static-method-in-async-tasks%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You are using the same instance of SplitService for all your tasks.
    Inside you are modifying it's member FatherProperties.



    Obviously, that's why you have this behavior.



    Create a service for each task:



    for (var nI = 0; nI < 10; nI++)

    var split = new SplitService(); // <-- a dedicated service for each

    var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
    var task = new Task (() => split.Split(fathers));
    tasks.Add(task);
    ;





    share|improve this answer























    • First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

      – user1425879
      Nov 13 '18 at 10:42












    • Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

      – Ofir Winegarten
      Nov 13 '18 at 10:50











    • Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

      – user1425879
      Nov 13 '18 at 11:52















    1














    You are using the same instance of SplitService for all your tasks.
    Inside you are modifying it's member FatherProperties.



    Obviously, that's why you have this behavior.



    Create a service for each task:



    for (var nI = 0; nI < 10; nI++)

    var split = new SplitService(); // <-- a dedicated service for each

    var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
    var task = new Task (() => split.Split(fathers));
    tasks.Add(task);
    ;





    share|improve this answer























    • First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

      – user1425879
      Nov 13 '18 at 10:42












    • Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

      – Ofir Winegarten
      Nov 13 '18 at 10:50











    • Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

      – user1425879
      Nov 13 '18 at 11:52













    1












    1








    1







    You are using the same instance of SplitService for all your tasks.
    Inside you are modifying it's member FatherProperties.



    Obviously, that's why you have this behavior.



    Create a service for each task:



    for (var nI = 0; nI < 10; nI++)

    var split = new SplitService(); // <-- a dedicated service for each

    var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
    var task = new Task (() => split.Split(fathers));
    tasks.Add(task);
    ;





    share|improve this answer













    You are using the same instance of SplitService for all your tasks.
    Inside you are modifying it's member FatherProperties.



    Obviously, that's why you have this behavior.



    Create a service for each task:



    for (var nI = 0; nI < 10; nI++)

    var split = new SplitService(); // <-- a dedicated service for each

    var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
    var task = new Task (() => split.Split(fathers));
    tasks.Add(task);
    ;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 9:27









    Ofir WinegartenOfir Winegarten

    7,46621221




    7,46621221












    • First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

      – user1425879
      Nov 13 '18 at 10:42












    • Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

      – Ofir Winegarten
      Nov 13 '18 at 10:50











    • Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

      – user1425879
      Nov 13 '18 at 11:52

















    • First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

      – user1425879
      Nov 13 '18 at 10:42












    • Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

      – Ofir Winegarten
      Nov 13 '18 at 10:50











    • Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

      – user1425879
      Nov 13 '18 at 11:52
















    First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

    – user1425879
    Nov 13 '18 at 10:42






    First of all thanks. That was the problem here. I think the code that I made as a sample of the real one does not represent entirely the problem. I have DI making AddTransient of the splitService so it should be always a new instance.

    – user1425879
    Nov 13 '18 at 10:42














    Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

    – Ofir Winegarten
    Nov 13 '18 at 10:50





    Hi, You welcome. You should open a new question with all the details and a A Minimal, Complete, and Verifiable example of the problematic DI code

    – Ofir Winegarten
    Nov 13 '18 at 10:50













    Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

    – user1425879
    Nov 13 '18 at 11:52





    Hi, I will produce a new sample with the same problem that I have and open a new question. Thanks for your help and time.

    – user1425879
    Nov 13 '18 at 11:52













    0














    First: Your excessive use of extension methods make the code very hard to read.

    Second: You are pulling away the very collection you are using th compare. You use only ONE SplitService and its FatherProperties and you are changeing them in every call to split.Split(fathers). Since you are doing it multithreaded, you compare in one thread while another just puts a new fatherproperties collection in there. When using the GetFatherValues() again you are avoiding this behavior and therefore not getting an error.



    Put the creation of the splitservice into the iteration.



    static void Main(string args)

    var tasks = new List<Task>();
    for (var nI = 0; nI < 100; nI++)

    var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
    var split = new SplitService();
    var task = new Task(() => split.Split(fathers));
    tasks.Add(task);
    ;

    foreach (var task in tasks)

    task.Start();


    Console.ReadKey();






    share|improve this answer





























      0














      First: Your excessive use of extension methods make the code very hard to read.

      Second: You are pulling away the very collection you are using th compare. You use only ONE SplitService and its FatherProperties and you are changeing them in every call to split.Split(fathers). Since you are doing it multithreaded, you compare in one thread while another just puts a new fatherproperties collection in there. When using the GetFatherValues() again you are avoiding this behavior and therefore not getting an error.



      Put the creation of the splitservice into the iteration.



      static void Main(string args)

      var tasks = new List<Task>();
      for (var nI = 0; nI < 100; nI++)

      var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
      var split = new SplitService();
      var task = new Task(() => split.Split(fathers));
      tasks.Add(task);
      ;

      foreach (var task in tasks)

      task.Start();


      Console.ReadKey();






      share|improve this answer



























        0












        0








        0







        First: Your excessive use of extension methods make the code very hard to read.

        Second: You are pulling away the very collection you are using th compare. You use only ONE SplitService and its FatherProperties and you are changeing them in every call to split.Split(fathers). Since you are doing it multithreaded, you compare in one thread while another just puts a new fatherproperties collection in there. When using the GetFatherValues() again you are avoiding this behavior and therefore not getting an error.



        Put the creation of the splitservice into the iteration.



        static void Main(string args)

        var tasks = new List<Task>();
        for (var nI = 0; nI < 100; nI++)

        var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
        var split = new SplitService();
        var task = new Task(() => split.Split(fathers));
        tasks.Add(task);
        ;

        foreach (var task in tasks)

        task.Start();


        Console.ReadKey();






        share|improve this answer















        First: Your excessive use of extension methods make the code very hard to read.

        Second: You are pulling away the very collection you are using th compare. You use only ONE SplitService and its FatherProperties and you are changeing them in every call to split.Split(fathers). Since you are doing it multithreaded, you compare in one thread while another just puts a new fatherproperties collection in there. When using the GetFatherValues() again you are avoiding this behavior and therefore not getting an error.



        Put the creation of the splitservice into the iteration.



        static void Main(string args)

        var tasks = new List<Task>();
        for (var nI = 0; nI < 100; nI++)

        var fathers = new List<Father> new Father Id = Guid.NewGuid() ;
        var split = new SplitService();
        var task = new Task(() => split.Split(fathers));
        tasks.Add(task);
        ;

        foreach (var task in tasks)

        task.Start();


        Console.ReadKey();







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 9:43

























        answered Nov 13 '18 at 9:14









        AndreasAndreas

        517211




        517211



























            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%2f53277323%2fpassing-instance-property-to-static-method-in-async-tasks%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