Passing instance property to static method in Async Tasks
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
add a comment |
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
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 doesGives 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
add a comment |
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
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
c# asynchronous static
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 doesGives 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
add a comment |
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 doesGives 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
add a comment |
2 Answers
2
active
oldest
votes
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);
;
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
add a comment |
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();
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
;
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
add a comment |
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);
;
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
add a comment |
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);
;
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);
;
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
add a comment |
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
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
edited Nov 13 '18 at 9:43
answered Nov 13 '18 at 9:14
AndreasAndreas
517211
517211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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