2012-09-27 65 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

相關問題