如何实现异步。完成事件在.NET外部使用? [英] How to implement async. completed event for external usage in .NET?
问题描述
我有这个简单的项目抢HTML元素的内部数据。这是一个DLL项目。
I have this simple project to grab HTML element inner data. This is a DLL Project.
public class WebGrabber
{
public string URL { set; get; }
public string Element { set; get; }
public bool FindByID { set; get; }
private WebBrowser b { set; get; }
private mshtml.IHTMLDocument2 doc { set; get; }
public void GetPageElementInnerHTML(string url, string element, bool findById)
{
URL = url;
Element = element;
FindByID = findById;
b = new WebBrowser();
b.Navigate(url);
b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
}
void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
doc = (IHTMLDocument2)b.Document.DomDocument;
string result = "<html>";
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);
result += "<head>" + head.innerHTML + "</head>";
if (null != doc)
{
foreach (IHTMLElement element in doc.all)
{
if (element is mshtml.IHTMLDivElement)
{
dynamic div = element as HTMLDivElement;
if (FindByID)
{
string id = div.id;
if (id == Element)
{
result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>";
break;
}
}
else
{
string className = div.className;
if (className == Element)
{
result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>";
break;
}
}
}
}
}
doc.close();
}
}
我需要的是实现的访问的为字符串结果变量。
所以它可能是可能得到异步这个变量从其他项目中。
也许,我需要一些调用getResult();方法是什么?......
Perhaps I need some GetResult(); method?....
我该怎么办呢?
感谢您!
推荐答案
您可以通过事件处理程序作为参数,然后其结果在应用程序中的类库工作否O只是传递一个回调,并调用后下载完成。
You can pass the eventhandler as an argument then work with the result in the app no in the class library o just pass a callback and invoke after download is complete.
我是新来的,所以我希望我没有理解你想要的。
I'm new here so i hope i did understand what you wanted.
薪火evnthandler
Passing the evnthandler
公共无效GetPageElementInnerHTML(字符串URL,字符串元素,布尔findById,WebBrowserDocumentCompletedEventHandler DownloadComplete等)
public void GetPageElementInnerHTML(string url, string element, bool findById, WebBrowserDocumentCompletedEventHandler downloadComplete)
使用委托:
公共类WebGrabber {
public class WebGrabber {
public string URL { set; get; }
public string Element { set; get; }
public bool FindByID { set; get; }
private WebBrowser b { set; get; }
private mshtml.IHTMLDocument2 doc { set; get; }
public delegate void DownloadCompletedDelegate(string result);
private DownloadCompletedDelegate _downloadedComplete;
public void GetPageElementInnerHTML(string url, string element, bool findById, DownloadCompletedDelegate downloadComplete)
{
_downloadedComplete = downloadComplete;
URL = url;
Element = element;
FindByID = findById;
b = new WebBrowser();
b.Navigate(url);
b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
}
void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
doc = (IHTMLDocument2)b.Document.DomDocument;
string result = "<html>";
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);
result += "<head>" + head.innerHTML + "</head>";
if (null != doc)
{
foreach (IHTMLElement element in doc.all)
{
if (element is mshtml.IHTMLDivElement)
{
dynamic div = element as HTMLDivElement;
if (FindByID)
{
string id = div.id;
if (id == Element)
{
result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>";
break;
}
}
else
{
string className = div.className;
if (className == Element)
{
result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>";
break;
}
}
}
}
}
doc.close();
_downloadedComplete.Invoke(result);
}
}
在APP
GetPageElementInnerHTML(URL,元素,真/假,CompletedCallback);
GetPageElementInnerHTML(URL, element, true/false, CompletedCallback);
私人无效CompletedCallback(字符串结果) { //你的code
private void CompletedCallback(string result) { //your code
}
这篇关于如何实现异步。完成事件在.NET外部使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!