2012-09-27 67 views
1

我有这样的代码:我该如何更快更高效地做到这一点?

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秒。有没有办法让它变得更快?

+0

你有没有运行,即使最基本的测试,看看什么是最耗时? 'update()'做了什么? –

+0

@EdS。是的,我有。它从网站上阅读需要时间。我应该在问题中提出这个问题,对不起。更新只允许通过删除覆盖整个程序的组来阻止您进行访问。 – Frank

+0

“这是从网站上读取需要时间”---好吧,购买更快的宽带然后 – zerkms

回答

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

为什么要休息一下?就是想。 – Frank

+1

因为你避免阅读(甚至通过网络传输)hwid.txt的其余部分 - 这可能很大? – sehe

+1

哦,好点。但是HWID.txt文件只有4行。 – Frank

相关问题