C# 中的比较字符串
好的,我尝试每 15 秒比较两个字符串,然后更新信息框。
这是迄今为止我从网络获取文本文档并将其存储到字符串中的代码:
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;
}
这是我尝试比较字符串的代码。
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我并不是故意尖酸刻薄,但这样做的目的是什么:
如果你想要暂停,为什么不只是 Thread.Sleep(TimeSpan.FromSeconds(1)) 呢?您的代码示例没有太大意义。
I don't mean to be snarky but what is the purpose of:
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 ......)
为您提供更多选择。请参阅 MSDN 上的 String.Compare 方法
String.Compare(string1, string2 ......)
gives you more options.Refer to String.Compare Method on MSDN
我认为您的 CompareStrings() 方法应该是这样的:
此代码使用
Timer
每秒检查一次 URL。每当此文本文件的内容发生更改时,此代码都会弹出一个新的NowPlaying
窗口(这就是我认为您正在尝试做的事情),并将继续执行此操作,直到您设置>_比较
与假
。您还可能希望轮询 URL 的频率低于每秒一次,在这种情况下,您可以将
timer.Interval
设置为 10000(10 秒)之类的值。I think your CompareStrings() method should be something like this:
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 newNowPlaying
window (which is what I think you're trying to do), and will continue to do this until you set_Comparing
tofalse
.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).用于检查正在播放的歌曲的线程应该与主应用程序线程分开,因为它会休眠,并且您希望(我认为)您的应用程序即使在检查之间也能保持响应。
编辑:比较现在应该可以正常工作(未经测试)。
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: