-2

Example of my code:

string value = client.DownloadString("http://www.site.com/value.html"); // There is a value of something like a1x6b2xs2 (just one line with random numbers that changes randomly).
Console.WriteLine(value);
Console.ReadKey();

That's all the code! I need to make the console write the number again only if the value in the site got changed! And make it a nonstop loop, just write the number again and again, but only if the old value is changed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1769042
  • 37
  • 1
  • 4
  • The question is not very clear to me. Did you mean something like : `int newValue = getValueFromHtml(); if (newValue != oldValue) display(); oldValue = newValue;` ? Or did you want to stop checking for differences regularly and be somehow informed that a change in the file occurred (which might or might not be feasible anyway) ? – Loomchild Oct 24 '12 at 14:53
  • I just edited the question so if someone can understand it now :P – user1769042 Oct 24 '12 at 15:48
  • Did you try any of the answers provided? I think most people understood what you were looking for, since everyone gave a similar answer. Even Loomchild. – Bobson Oct 24 '12 at 16:57

6 Answers6

7

Why don't you just keep the previous number in a variable and check that before printing?

int previousNumber = int.MinValue; // some invalid value 
while (isRunning) {
    // wait three minutes
    int currentNumber = GetNumberFromSite();
    if (currentNumber != previousNumber) {
        Console.WriteLine(currentNumber);
        previousNumber = currentNumber;
    }
}
Botz3000
  • 39,020
  • 8
  • 103
  • 127
1

You just need to store the old value, and check it before outputting the new one.

int oldVal = -1;

while (true)
{
  int newVal = GetNewValue();
  if (newVal != oldVal)
  {
     Console.WriteLine(newVal);
     oldVal = newVal ;
  }
  Thread.Sleep(1000 * 60 * 3)
}
Bobson
  • 13,498
  • 5
  • 55
  • 80
1

Something like this:

int current = ...;
int prev = current;
while(true)
{
    // get new number in current
    if(current != prev) 
        Console.WriteLine(current);
    prev = current;
    // wait 3 min
}
Tudor
  • 61,523
  • 12
  • 102
  • 142
1
int? oldNumber = null;


// in your loop reading from the website:
if (oldNumber != null && newNumber != oldNumber)
{
    oldNumber = newNumber;
    Console.WriteLine(newNumber);
}
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

You could use a timer that periodically checks whether or not that number has changed.

private System.Threading.Timer checkNumberTimer;
private int currentNumber;

private void InitTimer()
{
    //start immediately, interval is in TimeSpan
    checkNumberTimer = new System.Threading.Timer(
        timer_Elapsed,
        null,
        TimeSpan.Zero,
        Properties.Settings.Default.Interval
    );
}

private void timer_Elapsed(Object state)
{
    int oldNumber = currentNumber;
    currentNumber  =  CheckNumber();
    if( oldNumber != currentNumber  )
        ShowNewNumber( currentNumber );
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Write the value to a var, then check if it has changed?

int old, new
new = getValue(file.html)
if new != old then
  write(new) 
old = new

You can translate it to working code yourself.

Lodewijk
  • 100
  • 8