Problem abt threading!!
-
Salam we have a public static variable lets say temp in a class lets say A and another class lets say B creates a thread that will update temp's value after every 10 seconds by getting values from Data base.And another thread of the same class uses updated values of temp.Now the problem is this that , we are not getting updated values of temp in the second thread of the classB.can any body figure out the problem in this scenario. plz I need an urgent reply. thanks
-
Salam we have a public static variable lets say temp in a class lets say A and another class lets say B creates a thread that will update temp's value after every 10 seconds by getting values from Data base.And another thread of the same class uses updated values of temp.Now the problem is this that , we are not getting updated values of temp in the second thread of the classB.can any body figure out the problem in this scenario. plz I need an urgent reply. thanks
-
Use
lock
when you access the variable, or declare it asvolatile
.samtam wrote:
I need an urgent reply.
Are you sure that the reply should be urgent, or is it perhaps that it is needed urgently? ;)
--- b { font-weight: normal; }
-
Thanks, But we have already used Monitor.Enter and Monitor.Exit methods for locking mechanism, but it doesnt work. So now??????????? Urgent Reply is needed urgently!!!!!!!!!!!
-
That should do it. Are you using it both when reading and writing the value? Are you locking on the same object?
--- b { font-weight: normal; }
-
Ofcourse we are locking the same object and using it both reading and writing times. we used Monitor.TryEnter at the time of reading that object. which provides exclusive lock.
-
If you have done it right, then it works. So the conclusion is...? ;) What does the code look like?
--- b { font-weight: normal; }
its not possible to send u the whole code related to this coz its too long so only sending u chunks from it,i hope it will help u to understand our problem //this method is called after every 10 seconds by timer event public void ConnectDB(Object obj, EventArgs ev) { myTimer.Stop(); String query = "/////////////some selection from DB"; aReader = DB.ExecuteReader(query); projectDB.Clear(); while (aReader.Read()) { //structure is a Struct containing a string and an int variable //after gettings latest values from db in structure v add it // in projectDB arraylist structure.proj_name = aReader.GetString(0); structure.compl_status = aReader.GetInt32(1); //to lock the projectDB array list here Monitor.Enter(projectDB); projectDB.Add(structure); //to unlock the projectDB arraylist here Monitor.Exit(projectDB); } DB.Close(); if (!list_status.Contains(s))//some check i think u dnt need to understand it { exit_flag = true;//to disable timer } else { myTimer.Enabled = true;//to enable timer } } another method of the same class that is using value of this arraylist ///////////////code where v r reading values from projectDB if (Monitor.TryEnter(projectDB)) { //projectDB is an arraylist filled with info from database temp_projDB = (compile_status)projectDB[index]; } else continue; ////////////////// Thanks
-
its not possible to send u the whole code related to this coz its too long so only sending u chunks from it,i hope it will help u to understand our problem //this method is called after every 10 seconds by timer event public void ConnectDB(Object obj, EventArgs ev) { myTimer.Stop(); String query = "/////////////some selection from DB"; aReader = DB.ExecuteReader(query); projectDB.Clear(); while (aReader.Read()) { //structure is a Struct containing a string and an int variable //after gettings latest values from db in structure v add it // in projectDB arraylist structure.proj_name = aReader.GetString(0); structure.compl_status = aReader.GetInt32(1); //to lock the projectDB array list here Monitor.Enter(projectDB); projectDB.Add(structure); //to unlock the projectDB arraylist here Monitor.Exit(projectDB); } DB.Close(); if (!list_status.Contains(s))//some check i think u dnt need to understand it { exit_flag = true;//to disable timer } else { myTimer.Enabled = true;//to enable timer } } another method of the same class that is using value of this arraylist ///////////////code where v r reading values from projectDB if (Monitor.TryEnter(projectDB)) { //projectDB is an arraylist filled with info from database temp_projDB = (compile_status)projectDB[index]; } else continue; ////////////////// Thanks
I see three problems with the code: 1. You clear the list outside the monitoring. 2. You enter and leave the monitoring for each item you add, leaving the list in different state every the time. 3. You never leave the monitoring when you read from the list.
--- b { font-weight: normal; }