简体   繁体   中英

Compare Strings in C#

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

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.

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

Refer to String.Compare Method on MSDN

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).

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM