C# thread functions not properly sharing a static data member
Posted
by
Umer
on Stack Overflow
See other posts from Stack Overflow
or by Umer
Published on 2011-03-17T06:57:19Z
Indexed on
2011/03/17
8:10 UTC
Read the original article
Hit count: 333
I have a class as following
public class ScheduledUpdater
{
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
GetAndSaveScheduledUpdate(ID)
}
private static void UpdateSchedulingRefTableInThrear()
{
UpdateSchedulingRefTable();
}
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)
{
if (ScheduleConfiguration != null)
ScheduleConfiguration.Clear();
ScheduleConfiguration = result.Tables[0];
}
}
}
}
public static void GetAndSaveScheduledUpdate(int ID)
{
//use ScheduleConfiguration
if (ScheduleConfiguration == null)[1]
UpdateSchedulingRefTable();
DataRow[] result = ScheduleConfiguration.Select("ID = "+ID);
//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...
© Stack Overflow or respective owner