6 Replies Latest reply on Nov 28, 2017 12:45 PM by Rick Davin

    AFAttribute SetValue does not work correctly in a loop

    mark.omeara

       

       

      If I am writing values to AF (lets say they are just <none> data reference types, so internal attributes to AF) via the SDK in a loop, I find only some of the values are written, not all. For example

       
      for (int i = 0; i < 10000; i++)
      {
             MyAttribute.SetValue(new AFValue(i++, new OSIsoft.AF.Time.AFTime(aDateTime)));
           aDateTime = aDateTime.Add(new TimeSpan(1,0,0);
      }
      

      The above will not work until a Thread.Sleep is introduced:

       
      for (int i = 0; i < 10000; i++)
      {
             MyAttribute.SetValue(new AFValue(i++, new OSIsoft.AF.Time.AFTime(aDateTime)));
           aDateTime = aDateTime.Add(new TimeSpan(1,0,0);
           System.Threading.Thread.Sleep(100);
      }
      

       Now all the values are written.... surely I do not need to introduce arbitary sleeps into the code? What is going on?

       

       

        • Re: AFAttribute SetValue does not work correctly in a loop
          pcombellick

          Mark,

           

          What is the value of MyAttribute.IsConfigurationItem?  

           

          Regards,

           

          Paul

           

          AF Team

            • Re: AFAttribute SetValue does not work correctly in a loop
              mark.omeara

              The value is FALSE, it is not a configuration item.

                • Re: AFAttribute SetValue does not work correctly in a loop
                  pcombellick

                  Mark,

                   

                  I ran a modified version of your code:

                   
                                 AFElement el = AFDB.Elements.Add ( "EL" );
                  
                                 AFAttribute MyAttribute = el.Attributes.Add ( "atr1" );
                  
                                 MyAttribute.IsConfigurationItem = false;
                  
                                 el.CheckIn();
                  
                                 DateTime aDateTime = DateTime.Now;
                  
                                 for ( int i = 0; i < 10000; i++ )
                  
                                 {    
                  
                                     MyAttribute.SetValue ( new AFValue ( i++, new OSIsoft.AF.Time.AFTime(aDateTime)));    
                  
                                     aDateTime = aDateTime.Add(new TimeSpan(1,0,0));
                  
                                 }
                  
                                 for ( int i = 0; i < 10000; i++ )
                  
                                 {
                  
                                     MyAttribute.SetValue(new AFValue( i++, new OSIsoft.AF.Time.AFTime(aDateTime)));
                  
                                     aDateTime = aDateTime.Add(new TimeSpan(1, 0, 0));
                  
                                     Thread.Sleep(100);
                  
                                 }
                  

                   

                   

                  I am testing with AFSDK v2.5 and AFServer v2.5.  I monitored the traffic to SQL Server using Sql Profiler.  Both of these blocks of code made 5000 calls to the sql procedure to change a value, as I expected (notice that your code increments the value of "i", twice for each time around the loop).  I see the expected number rows in the change log and audit table.

                   

                  How did you determine that Sleep() is necessary?  What do you expect for successful behavior?

                   

                  Regards,

                   

                  Paul

                   

                  AF Dev