David G

David G

  • 1.5k
  • 224
  • 14.6k

Changing value in variable from another called class

Mar 29 2021 3:14 AM
Hey all I have been racking my brain trying to figure out what it is I am missing from the code below in order for the value "7:50 pm" to be placed into the variable theCntDownTimerTime from the TinyWS.cs class.
 
Let say the code below starts out with the time "7:00 pm".
 
I send the command from PostMAN (7:50 pm) and it picks it up just fine and the jsonSent.changeCNT does have the correct value of "7:50 pm". However, after this it seems to lose that value once the timer updates in the clockCoiuntDown.xaml.cs page for the theCntDownTimerTime.ToLower(). That value is still the same value (7:00 pm) as it was when the program started up and read my text file to get the default time.
clockCountDown.xaml.cs
  1. namespace laptopWatcher {  
  2.   public partial class clockCountDown: Window {  
  3.     public string theCntDownTimerTime = "";  
  4.   
  5.     public clockCountDown() {  
  6.       InitializeComponent();  
  7.       //get countdown time  
  8.       theCntDownTimerTime = getTimeToStart();  
  9.     }  
  10.   
  11.     private void Window_Loaded(object sender, RoutedEventArgs e) {  
  12.       var desktopWorkingArea = SystemParameters.WorkArea;  
  13.       this.Left = desktopWorkingArea.Right - this.Width - 20;  
  14.       this.Top = desktopWorkingArea.Top + this.Height - 30;  
  15.       var ts = ((60 - DateTime.Now.Second) * 1000 - DateTime.Now.Millisecond);  
  16.   
  17.       clockTimer.Tick += new EventHandler(clockTimer_tick);  
  18.       clockTimer.Interval = new TimeSpan(0, 0, 35);  
  19.       clockTimer.Start();  
  20.   
  21.       //Populate the time once to show to user  
  22.       bgTxt.Text = DateTime.Now.ToString("h:mm tt");  
  23.       txt.Text = DateTime.Now.ToString("h:mm tt");  
  24.   
  25.       //check if time for count down  
  26.       if (txt.Text.ToLower().Equals(theCntDownTimerTime.ToLower())) {  
  27.         _timer.Stop();  
  28.         TimeSpan time = TimeSpan.Parse(theCntDownTimerTime);  
  29.         cdownTimer((_time).TotalSeconds);  
  30.       }  
  31.   
  32.       _tws = new TinyWS();  
  33.       Thread t = new Thread(new ThreadStart(_tws.startWS));  
  34.   
  35.       t.Start();  
  36.     }  
  37.   
  38.     private void clockTimer_tick(object sender, EventArgs e) {  
  39.       bgTxt.Text = DateTime.Now.ToString("h:mm tt");  
  40.       txt.Text = DateTime.Now.ToString("h:mm tt");  
  41.       Console.WriteLine(txt.Text.ToLower() + " vs " + theCntDownTimerTime.ToLower());  
  42.     }  
  43.   
  44.     public string getTimeToStart() {  
  45.       string line;  
  46.   
  47.       using(StreamReader sr = new StreamReader(Environment.CurrentDirectory + "\\timestart.txt")) {  
  48.         line = sr.ReadLine();  
  49.       }  
  50.   
  51.       return line.ToLower();  
  52.     }  
  53.   
  54.     // etc etc....  
  55.   }  
  56. }  
TinyWS.cs
  1. namespace laptopLogin {  
  2.   class TinyWS: clockCountDown {  
  3.     HttpListener listener = new HttpListener();  
  4.     clockCountDown ccd = new clockCountDown();  
  5.   
  6.     public class receivedData {  
  7.       public string changeCNT {  
  8.         get;  
  9.         set;  
  10.       }  
  11.       public int timeAdd {  
  12.         get;  
  13.         set;  
  14.       }  
  15.       public bool internet {  
  16.         get;  
  17.         set;  
  18.       }  
  19.       public bool shutdownNow {  
  20.         get;  
  21.         set;  
  22.       }  
  23.     }  
  24.   
  25.     public void startWS() {  
  26.       var prefixes = new List < string > () {  
  27.         "http://*:8888/"  
  28.       };  
  29.   
  30.       foreach(string s in prefixes) {  
  31.         listener.Prefixes.Add(s);  
  32.       }  
  33.   
  34.       listener.Start();  
  35.       Console.WriteLine("Listening...");  
  36.   
  37.       while (true) {  
  38.         HttpListenerContext context = listener.GetContext();  
  39.         HttpListenerRequest request = context.Request;  
  40.   
  41.         string documentContents;  
  42.   
  43.         using(Stream receiveStream = request.InputStream) {  
  44.           using(StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) {  
  45.             documentContents = readStream.ReadToEnd();  
  46.           }  
  47.         }  
  48.   
  49.         Console.WriteLine($"Recived request for {request.Url}");  
  50.         Console.WriteLine(documentContents);  
  51.         HttpListenerResponse response = context.Response;  
  52.   
  53.         //Check out the response  
  54.         receivedData jsonSent = JsonConvert.DeserializeObject < receivedData > (documentContents);  
  55.   
  56.         if (jsonSent.changeCNT.ToLower() != ccd.getTimeToStart()) {  
  57.           //The default shutdown time has changed so update the txt file.  
  58.           using(StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\timestart.txt"false)) {  
  59.             sw.WriteLine(jsonSent.changeCNT);  
  60.           }  
  61.   
  62.           ccd.theCntDownTimerTime = jsonSent.changeCNT;  
  63.         }  
  64.   
  65.         //Reply  
  66.         string responseString = "hit";  
  67.         byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);  
  68.         response.ContentLength64 = buffer.Length;  
  69.         Stream output = response.OutputStream;  
  70.         output.Write(buffer, 0, buffer.Length);  
  71.         output.Close();  
  72.       }  
  73.     }  
  74.   
  75.     public void stopWS() {  
  76.       listener.Stop();  
  77.     }  
  78.   }  
  79. }  
 
So what would I be missing? It looks fine to me but just doesn't store the value over.
 
I also have tried:
  1. public string theCntDownTimerTime = "";  
  2. public string _newCNT {  
  3.   get {  
  4.     return theCntDownTimerTime;  
  5.   }  
  6.   set {  
  7.     theCntDownTimerTime = value;  
  8.   }  
  9. }   
And on the TinyWS.cs page I have it sending by like ccd._newCNT = jsonSent.changeCNT;
But that also does not hold the new value.