Note that there are some explanatory texts on larger screens.

plurals
  1. POC# thread functions not properly sharing a static data member
    text
    copied!<p>I have a class as following</p> <pre><code>public class ScheduledUpdater { private static readonly object lockingObject = new object(); private static Queue&lt;int&gt; PendingIDs = new Queue&lt;int&gt;(); 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 &amp;&amp; ds.Tables.Count &gt; 0 &amp;&amp; ds.Tables[0].Rows.Count &gt; 0) { List&lt;string&gt; lstIDs = new List&lt;string&gt;(); for (int i = 0; i &lt; 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 &gt; 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 &amp;&amp; result.Tables.Count &gt; 0) { ***[2]*** Monitor.Enter(lockingObject); if (ScheduleConfiguration != null) ScheduleConfiguration.Clear(); 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 } } </code></pre> <p>The function <code>UpdateSchedulingRefTable</code> can be called any time from outside world (for instance if someone updates the schedule configuration manually) <code>ProcessScheduledUpdates</code> is called from a windows service every other minute. Problem: Datatable <code>ScheduleConfiguration</code> is updated in the <code>UpdateSchedulingRefTable</code> (called from outside world - say manually) but when i try to use Datatable <code>ScheduleConfiguration</code> in <code>GetAndSaveScheduledUpdate</code>, i get the older version of values....</p> <p>What am I missing in this stuff???</p> <p><strong>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...</strong></p> <p><strong>Edit 2</strong>: <code>ProcessScheduledUpdates</code> is called from a windows service (say every second). I am first hitting point <strong>[1]</strong> (it will automatically hit point <strong>[2]</strong> and update <code>ScheduleConfiguration</code>), then i <strong>manually update DB</strong> and hit point <strong>[2]</strong>. Then i hit point <strong>[1]</strong> again to just check the status My problem is now, that i get two entirely different copies of <code>ScheduleConfiguration</code> at point [1] and [2]. </p> <p><strong>Edit 3</strong> 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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload