When run conflict between class variables!
private myClass[] arrayms = new myClass[5];
foreach (myClass ms in arrayms) {
if (ms.ScheduleState)
Task.Factory.StartNew(() => ms.Start());
}
When run conflict between class variables!
private myClass[] arrayms = new myClass[5];
foreach (myClass ms in arrayms) {
if (ms.ScheduleState)
Task.Factory.StartNew(() => ms.Start());
}
I got a text summary, Classes are object. program do not have error, but only conflict variables when run. –
I don't understand "conflict variables". But the rest seems to fit what I described in my previous comment. i.e. the array is empty when calling foreach
. So maybe this:
for (int i=0; i < ms.Length; i++) {
arrayms[i] = new MyClass();
}
// ScheduleState must somehow get set, then:
foreach (myClass ms in arrayms) {
if (ms.ScheduleState)
Task.Factory.StartNew(() => ms.Start());
}
edit
what does Task.Factory.StartNew(..) do? Its name StartNew implies that a new object is created and started. But an anonymous method that starts an existing object is passed. So are we actually starting two things here?
if this (comment, below) is not the problem then maybe it's the infamous scope problem when calling LINQ in a loop:
foreach (myClass ms in arrayms) {
MyClass workAround = ms; // must set "ms" to a loop-scoped variable.
// why? the answer is TL;DR
if (workAround.ScheduleState)
Task.Factory.StartNew(() => workAround.Start());
end edit
How can I run Several from a function at the same time from one class? C# When run _StartTask() conflict between myClass variables and log result messy
private myClass[] arrayms = new myClass[5];
public void _TasksClassCreator()
{
foreach (var ms in arrayms )
{
ms.ScheduleName = SName;
.
.
.
}
}
public void _StartTask()
{
foreach (myClass ms in arrayms)
{
if (ms.ScheduleState)
Task.Factory.StartNew(() => ms.Start());
}
}
public sealed class myClass
{
public void Start()
{
_TBTask();
}
private void _TBTask()
{
while(true)
{
...//Conflict here
// this function always running and reporting result...
//log here
}
}
private string _ScheduleName;
public string ScheduleName
{
get
{
return _ScheduleName;
}
set
{
_ScheduleName = value;
}
.
.
.
}
}