tags:

views:

350

answers:

5

Ok,I am trying to compare two strings every 15 seconds and then update an info box.

Here is the code I have so far to get a text document from the web and store it into a string:

public String GetData(String url)
{
    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    String data = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    response.Close();
    return data;
}

And here is what I have with trying to compare the strings.

public void CompareStrings()
{
    int x;
    x = 1;
    String data = GetData("http://xcastradio.com/stats/nowplaying.txt");
    string savedData = data;
    while (x > 0 && x < 100000001)
    {
        x++;
    }
    String data1 = GetData("http://xcastradio.com/stats/nowplaying.txt");
    NowPlayingInfo1.Text = data;
    NowPlaying np = new NowPlaying();
    if (data1 != savedData)
    {
        NowPlayingInfo1.Text = data1;
        np.Show(this);
    }
}
+1  A: 
public void CompareStrings()
    {
        String data = GetData("http://xcastradio.com/stats/nowplaying.txt");

        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));

        String data1 = GetData("http://xcastradio.com/stats/nowplaying.txt");

        NowPlayingInfo1.Text = data;

        NowPlaying np = new NowPlaying();

        if (String.Compare(data, data1) != 0)
        {
            NowPlayingInfo1.Text = data1;
            np.Show(this);
        }

    }

This thread to check the song now playing should be separate from the main application thread, since it sleeps and you want (I think) for your app to keep responding even between checks.

Edit: Compare should now work correctly (not tested).

Zach
+6  A: 

I don't mean to be snarky but what is the purpose of:

    while (x > 0 && x < 100000001)
    {
        x++;
    }

If you want a pause, why not just Thread.Sleep(TimeSpan.FromSeconds(1))? Your code sample doesn't make too much sense.

David in Dakota
@David: Precisely, though nothing to with with string compare, looping to 'delay' is one of the developers' "No-No"s.
o.k.w
@okw: using `Thread.Sleep(n)` to delay is almost as big of a no-no as looping (although at least it doesn't produce a processor-dependent duration).
MusiGenesis
I was offline at the time and couldn't figure out a way to get it to stop at an interval. So making my computer count was just to see if what I wrote would work how I wanted it to at the time.
Cistoran
@Musi: Using Sleep is just fine, so long as you're doing it in a background thread.
Steven Sudit
+4  A: 

String.Compare(string1, string2 ......) gives you more options.

Refer to String.Compare Method on MSDN

o.k.w
+3  A: 

I think your CompareStrings() method should be something like this:

private bool _Comparing = false;
private string _URL = "http://xcastradio.com/stats/nowplaying.txt";
private string _data = "";
public void CompareStrings()
{
    Timer timer = new Timer();
    timer.Interval = 1000;
    timer.Tick += timer_Tick;
    _data = GetData(_URL);
    _Comparing = true;
    timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
    if (_Comparing)
    {
        string newdata = GetData(_URL);
        if (newdata != _data)
        {
            NowPlaying np = new NowPlaying();
            NowPlayingInfo1.Text = newdata;
            _data = newdata;
            np.Show(this);
        }
    }
    else
    {
        Timer timer = (Timer)sender;
        timer.Stop();
    }
}

This code uses a Timer to check the URL once every second. Whenever the contents of this text file changes, this code will pop up a new NowPlaying window (which is what I think you're trying to do), and will continue to do this until you set _Comparing to false.

You also might want to poll the URL less frequently than once per second, in which case you would set timer.Interval to something like 10000 (10 seconds).

MusiGenesis
System.Windows.Forms.Timer is used in WinForms... he's doing Web.
balexandre
@balexandre: as far as I can tell, he's writing a winforms app that accesses a web resource. The key line in his original code is `np.Show(this);` which is a method used to show a `Form`.
MusiGenesis
A: 

I recommend you instead, utilize the following:

  1. Generate a hash of the saved data and store the value, you don't need to create large string objects if you don't need to really...
  2. For all new reads, simply generate a hash and compare that against the saved hash object.
  3. Use any hashing algorithm, but I would recommend using shah1.
  4. Use the built in String.Compare(...) method of the String class to compare the hash objects.
  5. Try Thread.Sleep([millisecondsvaluehere]) to pause program execution. You could also consider putting the read call in a timer, forms or system timer (make sure to invoke before accessing UI objects)