locked
Static Variables from Static Class not updated RRS feed

  • Question

  • User659967990 posted

    Hello,

    I have quartz .net n my MVC application to execute job at 1 minute interval, my job sets value to my static variables present in static global class.

    I am then passing static variable values to my html element at javascript setinterval() but in my case values in static varaible are not getting updated.

    But when i place breakpoint to quartz job I can see static variable value is being set, Please help me where I am doing wrong

    setInterval(function () {
            var x =  @AppName.Globals.Variable1;
            var y = @AppName.Globals.Variable2;
            var z = @AppName.Globals.Variable3;
            $("#Knob1").val(y).trigger('change');
            $("#Knob2").val(z).trigger('change');
            $("#Knob3").val(x).trigger('change');
        },5000);
    
    
    
    //My Quartz Job at every 1 Minute
    public void Execute(IJobExecutionContext context)
            {
                try
                {
                    Random rnd = new Random();
                    Globals.Variable1 = rnd.Next(1, 100);;
                    Globals.Variable2 = rnd.Next(1, 200);;
                    Globals.Variable3 = rnd.Next(1, 300);;
                }
                catch (Exception ex) { string errormsg = ex.ToString(); }
            }
    
    
    //My Global Static Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace AppName
    {
        public static class Globals
        {
            public static double Variable1 { get; set; }
            public static double Variable2 { get; set; }
            public static double Variable3 { get; set; }
        }
    }

    Wednesday, November 28, 2018 11:41 AM

All replies

  • User-474980206 posted
    The c# static values are only accessed when the view is rendered. The JavaScript variables in are in the browsers JavaScript instance. Your setInverval will need to make an Ajax call to get the current values.
    Wednesday, November 28, 2018 2:59 PM
  • User1520731567 posted

    Hi ParthDesai,

    For example,you could refer to this demo:

    setInterval(function(){ alert("Hello"); }, 3000);

    It will wait 3 seconds and alert "Hello" at the first time,and then an new alert box will appear in 3 seconds.

    I am then passing static variable values to my html element at javascript setinterval() but in my case values in static varaible are not getting updated.

    Actually,your case values will get updated after 5 seconds at the first time,and get updated in 5 seconds ...rather then 'are not getting updated.'

    Best Regards.

    Yuki Tao

    Thursday, November 29, 2018 9:17 AM