[英]Event Calling from a method c#
我想从SendNotification方法中的this.Job.JobCompleted调用Job_JobCompleted事件。 但是this.job向我显示了null,因此我无法调用它。 请帮助我或以任何方式调用Job_JobCompleted事件。 谢谢
public override void SendNotification(string content)
{
this.Job.JobCompleted += Job_JobCompleted;
}
void Job_JobCompleted(object sender, JobCompletionData e)
{
//List<string> crawledUrls = new List<string>(); // List of included URLS in c#
//crawledUrls.Add("http://www.friferie.dk/inspiration/India");
//crawledUrls.Add("http://www.friferie.dk/inspiration/UK");
//crawledUrls.Add("http://www.friferie.dk/inspiration/Spain");
//crawledUrls.Add("http://www.friferie.dk/inspiration/Nigeria");
//crawledUrls.Add("http://www.friferie.dk/inspiration/Uganda");
string comment = @"sitemap-generator-url=http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine + Environment.NewLine +
"This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine + Environment.NewLine +
"Audit My PC also offers free security tools to help keep you safe during internet travels";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement root = new XElement("urlset",
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XComment(comment));
List<XElement> ChildNodes = new List<XElement>();
foreach (string url in e.CrawledUrls)
{
ChildNodes.Add(CreateXMLNode(url));
}
root.Add(ChildNodes);
root.Save("d:/product.xml");
}
public static XElement CreateXMLNode(string url)
{
XElement urlnode = new XElement("url");
urlnode.Add(new XElement("loc", url));
return urlnode;
}
传递此事件Job_JobCompleted(object sender,JobCompletionData e)所需的参数,在sender中传递引发该事件的按钮名称,并在e中传递新的EventArgs,应类似于Job_JobCompleted(btn,new EventArgs())
与代码
this.Job.JobCompleted += Job_JobCompleted;
您只需挂接事件。 您没有调用该事件。 重新编写代码并将“连接”放置在其他位置(通常是在创建作业的时候)
public void StartupJob()
{
this.Job = new Job(#params); //create the object
this.Job.JobCompleted += Job_JobCompleted; //hookup the event
this.Job.Run(); //do some work
}
public override void SendNotification(string content)
{
//Invoke your event (always see if it is hooked up)
if(Job.JobCompleted != null)
Job.JobCompleted(this, new JobCompletionData(content));
}
我不知道你的JobCompletionData是什么样子,但它可能有一个构造函数...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.