R
rick
I have a service that runs jobs. Each job is an instance of a class
that inherits a CJob class. The service has a collection of objects
(job classes). Each job has an inherited property named Running from
the CJob class. The service checks the status of each jobs Running
property every second. Each job class has a Run method.
Is it possible that the service gets confused and misreads the Running
property? In other words, the code looks like it is looking at the
Running property of Job1 but in fact it is looking at Job2. My partner
believes this can happen due to multiple CPUs. Is he crazy? Or am I
stupid? It is probably the later.
namespace KPI
{
public abstract class CJob
{
public override void Run() {}
private bool mbRunning;
public bool Running {
get { return mbRunning; }
set { mbRunning = value; }
}
}
public class CExportSales : CJob
{
public CExportSales() {}
public override void Run() { // job code written here }
}
}
// SERVICE
namespace KPIService
{
class KPIServiceMain
{
private void RunJobs()
{
int iThreads = 0;
foreach (CJob oJob in moJobs.Values)
{
if(oJob.Enabled == 1) {
if (oJob.Running) {
iThreads++;
} else {
Thread oThread = new Thread(new
ThreadStart(oJob.Run));
oThread.Start();
}
if (iThreads >= 10) { break; }
}
}
}
}
}
/// The job objects are loaded from database records
oJob =
(CJob)Activator.CreateInstance(Type.GetType(dr["class"].ToString() +
", KPI"));
oJob.Name = dr["name"].ToString();
oJob.Enabled = Convert.ToInt32(dr["enabled"]);
moJobs.Add(oJob.Name, oJob);
that inherits a CJob class. The service has a collection of objects
(job classes). Each job has an inherited property named Running from
the CJob class. The service checks the status of each jobs Running
property every second. Each job class has a Run method.
Is it possible that the service gets confused and misreads the Running
property? In other words, the code looks like it is looking at the
Running property of Job1 but in fact it is looking at Job2. My partner
believes this can happen due to multiple CPUs. Is he crazy? Or am I
stupid? It is probably the later.
namespace KPI
{
public abstract class CJob
{
public override void Run() {}
private bool mbRunning;
public bool Running {
get { return mbRunning; }
set { mbRunning = value; }
}
}
public class CExportSales : CJob
{
public CExportSales() {}
public override void Run() { // job code written here }
}
}
// SERVICE
namespace KPIService
{
class KPIServiceMain
{
private void RunJobs()
{
int iThreads = 0;
foreach (CJob oJob in moJobs.Values)
{
if(oJob.Enabled == 1) {
if (oJob.Running) {
iThreads++;
} else {
Thread oThread = new Thread(new
ThreadStart(oJob.Run));
oThread.Start();
}
if (iThreads >= 10) { break; }
}
}
}
}
}
/// The job objects are loaded from database records
oJob =
(CJob)Activator.CreateInstance(Type.GetType(dr["class"].ToString() +
", KPI"));
oJob.Name = dr["name"].ToString();
oJob.Enabled = Convert.ToInt32(dr["enabled"]);
moJobs.Add(oJob.Name, oJob);