-2

我的代码示例:

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();

这就是所有的代码!仅当站点中的值发生更改时,我才需要让控制台再次写入数字!并使其成为一个不间断的循环,只需一次又一次地写入数字,但前提是旧值已更改。

4

6 回答 6

7

为什么不将前一个数字保存在变量中并在打印前检查它?

int previousNumber = int.MinValue; // some invalid value 
while (isRunning) {
    // wait three minutes
    int currentNumber = GetNumberFromSite();
    if (currentNumber != previousNumber) {
        Console.WriteLine(currentNumber);
        previousNumber = currentNumber;
    }
}
于 2012-10-24T14:49:11.387 回答
1

您只需要存储旧值,并在输出新值之前对其进行检查。

int oldVal = -1;

while (true)
{
  int newVal = GetNewValue();
  if (newVal != oldVal)
  {
     Console.WriteLine(newVal);
     oldVal = newVal ;
  }
  Thread.Sleep(1000 * 60 * 3)
}
于 2012-10-24T14:49:37.907 回答
1

像这样的东西:

int current = ...;
int prev = current;
while(true)
{
    // get new number in current
    if(current != prev) 
        Console.WriteLine(current);
    prev = current;
    // wait 3 min
}
于 2012-10-24T14:49:47.063 回答
1
int? oldNumber = null;


// in your loop reading from the website:
if (oldNumber != null && newNumber != oldNumber)
{
    oldNumber = newNumber;
    Console.WriteLine(newNumber);
}
于 2012-10-24T14:50:19.750 回答
0

将值写入 var,然后检查它是否已更改?

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

您可以自己将其转换为工作代码。

于 2012-10-24T14:53:10.057 回答
0

您可以使用 atimer定期检查该数字是否已更改。

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 );
}
于 2012-10-24T14:51:32.400 回答