如何更快更有效地完成这项工作?

时间:2012-09-27 23:53:20

标签: c#

我有这段代码:

var url = "myurl.com/hwid.txt";
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
  string downloadedString;
  while ((downloadedString = reader.ReadLine()) != null)
  {
    if (downloadedString == finalHWID)
    {
      update();
      allowedIn = true;
    }
  }
  if (allowedIn == false)
  {
    MessageBox.Show("You are not allowed into the program!", name, 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
  }

这将根据允许的列表检查您的HWID。但是,每次完成检查大约需要5-10秒。有没有办法让它变得更快?

1 个答案:

答案 0 :(得分:2)

找到匹配后你可以break

var url = "myurl.com/hwid.txt";
var client = new WebClient();

using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
    string downloadedString;
    while ((downloadedString = reader.ReadLine()) != null)
    {
        if (downloadedString == finalHWID)
        {
            update();
            allowedIn = true;
            break;
        }
    }
}

if (allowedIn == false)
{
    MessageBox.Show("You are not allowed into the program!", name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}