2013-03-23 52 views
-1

這裏的想法是允許調用者爲循環的每個循環實現AfterHTMLPieceHandler,我不知道它是不是正確的,或者它應該是一個事件或委託。老實說,我不知道區別,但這是另一個問題。如何向調用者實現公開事件,請參閱代碼示例?

//? Should it be static? 
public static class PageScan 
{ 
    public delegate string AfterHTMLPieceHandler(); 

    public static string GetHTML(string url) 
    { 
     HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url); 
     wRequest.Method = "GET"; 
     HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse(); 
     StreamReader sReader = new StreamReader(wResponse.GetResponseStream()); 
     string html = sReader.ReadToEnd(); 
     sReader.Close(); 
     wResponse.Close(); 
     sReader.Dispose(); 
     return html; 
    } 

    public static void GetPiecesOfHtml(string html, string constantHtml) 
    { 
     while (html.IndexOf("http://portfoliopad.com/images/") > -1) 
     { 
      // it will retrieve a piece of html given the constantHtml and remove from html in order to break the loop in the end 
      //How Do I hit the event for each time one cycle of the loop ends? 
     } 
    } 
} 
+0

真棒,感謝您的downvote沒有評論,你讓stackoverflow更豐富 – RollRoll 2013-03-23 23:25:06

+0

你正在創建一個委託,但它不是指向任何事件,我可以看到..你看了起來如何做匿名代表或如何使用/創建代表..? – MethodMan 2013-03-23 23:54:49

回答

0

這是關於使用代表的一個很好的參考:Understanding Delegates。記下示例主要部分中的委託實例,以及它如何與委託所指向的方法綁定。我希望這有幫助。