Reputation: 1597
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);
}
}
Upvotes: 1
Views: 889
Reputation: 69
I recommend you instead, utilize the following:
Upvotes: 0
Reputation: 459
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).
Upvotes: 1
Reputation: 75296
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).
Upvotes: 3
Reputation: 25790
String.Compare(string1, string2 ......)
gives you more options.
Refer to String.Compare Method on MSDN
Upvotes: 4
Reputation: 10124
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.
Upvotes: 6