Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem abt threading!!

Problem abt threading!!

Scheduled Pinned Locked Moved C#
helpannouncement
8 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samtam
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • S samtam

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use lock when you access the variable, or declare it as volatile.

      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; }

      S 1 Reply Last reply
      0
      • G Guffa

        Use lock when you access the variable, or declare it as volatile.

        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; }

        S Offline
        S Offline
        samtam
        wrote on last edited by
        #3

        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!!!!!!!!!!!

        G 1 Reply Last reply
        0
        • S samtam

          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!!!!!!!!!!!

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          S 1 Reply Last reply
          0
          • G Guffa

            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; }

            S Offline
            S Offline
            samtam
            wrote on last edited by
            #5

            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.

            G 1 Reply Last reply
            0
            • S samtam

              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.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              If you have done it right, then it works. So the conclusion is...? ;) What does the code look like?

              --- b { font-weight: normal; }

              S 1 Reply Last reply
              0
              • G Guffa

                If you have done it right, then it works. So the conclusion is...? ;) What does the code look like?

                --- b { font-weight: normal; }

                S Offline
                S Offline
                samtam
                wrote on last edited by
                #7

                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

                G 1 Reply Last reply
                0
                • S samtam

                  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

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  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; }

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups