C# thread functions not properly sharing a static data member
I have a class as following
public class ScheduledUpdater
{
private static readonly object lockingObject = new object();
private static Queue<int> PendingIDs = new Queue<int>();
private static bool UpdateThreadRunning = false;
private static bool IsGetAndSaveScheduledUpdateRunning = false;
private static DataTable ScheduleConfiguration;
private static Thread updateRefTableThread;
private static Thread threadToGetAndSaveScheduledUpdate;
public static void ProcessScheduledUpdates(int ID)
{
//do some stuff
// if ( updateRefTableThread not already running)
// execute updateRefTableThread = new Thread(new ThreadStart(UpdateSchedulingRefTableInThrear));
// execute updateRefTableThread.Start();
//do some stuff
***[1]***
GetAndSaveScheduledUpdate(ID)
}
private static void UpdateSchedulingRefTableInThrear()
{
//if(updateRefTableThread==true)
// return;
// updateRefTableThread = true
do{
UpdateSchedulingRefTable();
Thread.sleep(800000);
}while(updateRefTableThread);
//updateRefTableThread = false;
}
public static void UpdateSchedulingRefTable()
{
// read DB and update ScheduleConfiguration
string query = " SELECT ID,TimeToSendEmail FROM TBLa WHERE MODE = 'WebServiceOrder' AND BDELETE = false ";
clsCommandBuilder commandBuilder = new clsCommandBuilder();
DataSet ds = commandBuilder.GetDataSet(query);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
List<string> lstIDs = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lstIDs.Add(ds.Tables[0].Rows[i]["ID"].ToString());
if (LastEmailSend.Contains(ds.Tables[0].Rows[i]["ID"].ToString()))
LastEmailSend[ds.Tables[0].Rows[i]["ID"].ToString()] = ds.Tables[0].Rows[i]["TimeToSendEmail"].ToString();
else
LastEmailSend.Add(ds.Tables[0].Rows[i]["ID"].ToString(), ds.Tables[0].Rows[i]["TimeToSendEmail"].ToString());
}
if (lstIDs.Count > 0)
{
string Ids = string.Join(",", lstIDs.ToArray()).Trim(',');
dhDBNames dbNames = new dhDBNames();
dbNames.Default_DB_Name = dbNames.ControlDB;
dhGeneralPurpose dhGeneral = new dhGeneralPurpose();
dhGeneral.StringDH = Ids;
DataSet result = commandBuilder.GetDataSet(dbNames, (object)dhGeneral, "xmlGetConfigurations");
if (result != null && result.Tables.Count > 0)
{
***[2]***
Monitor.Enter(lockingObject);
if (ScheduleConfiguration != null)
ScheduleConfiguration.Clear();
开发者_StackOverflow社区 ScheduleConfiguration = result.Tables[0];
Monitor.Exit(lockingObject);
}
}
}
}
public static void GetAndSaveScheduledUpdate(int ID)
{
***[3]***
//use ScheduleConfiguration
Monitor.Enter(lockingObject);
if (ScheduleConfiguration == null)
{
UpdateSchedulingRefTable();
Monitor.Exit(lockingObject);
}
else
Monitor.Exit(lockingObject);
Monitor.Enter(lockingObject);
DataRow[] result = ScheduleConfiguration.Select("ID = "+ID);
Monitor.Exit(lockingObject);
//then for each result row, i add this to a static Queue PendingIDs
}
}
The function UpdateSchedulingRefTable
can be called any time from outside world (for instance if someone updates the schedule configuration manually)
ProcessScheduledUpdates
is called from a windows service every other minute.
Problem:
Datatable ScheduleConfiguration
is updated in the UpdateSchedulingRefTable
(called from outside world - say manually) but when i try to use Datatable ScheduleConfiguration
in GetAndSaveScheduledUpdate
, i get the older version of values....
What am I missing in this stuff???
About EDIT: I thought the stuff i have not shown is quite obvious and possibly not desired, perhaps my structure is wrong :) and sorry for incorrect code previously, i made a simple function call as a thread initialization... sorry for my code indentation too because i don't know how to format whole block...
Edit 2: ProcessScheduledUpdates
is called from a windows service (say every second).
I am first hitting point [1] (it will automatically hit point [2] and update ScheduleConfiguration
), then i manually update DB and hit point [2]. Then i hit point [1] again to just check the status
My problem is now, that i get two entirely different copies of ScheduleConfiguration
at point [1] and [2].
Edit 3 I think my question is invalid; i wanted to share data betweena a windows form click and a windows service. No matter how many shared things i have, atleast the address space will be different... My bad...
Well, for one thing there's no sign that you've got any memory barriers anywhere which would ensure that any writes are fully "flushed" to main memory, and that reads go to main memory instead of using registers etc. Basically you shouldn't use fields shared between threads without some sort of protection.
Now that's a theoretical risk to some extent - it's not clear that that's actually the problem, although it's certainly a real possibility. I strongly suspect there's another problem, but in the code that you haven't shown us. Do you have diagnostics to indicate that the first thread definitely wrote to ScheduleConfiguration
?
精彩评论