2015-10-14 54 views
0

我想從SendNotification方法中的this.Job.JobCompleted中調用Job_JobCompleted事件。但是,這個.job顯示我爲空,所以我無法稱呼它。請以任何方式幫助我或以任何方式致電Job_JobCompleted事件。謝謝從方法c調用事件#

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

只能在聲明事件的類內調用事件。請參閱[事件教程](https://msdn.microsoft.com/en-us/library/aa645739(v = vs.71).aspx)。你正在嘗試提升'Job'屬性中的某個對象的'JobCompleted'事件。你不能這樣做。只有'Job'對象可以引發這個事件。什麼應該引發這個事件? –

回答

0

路過此事件Job_JobCompleted(對象發件人,JobCompletionData E)預計參數時,在發送者通過它提高該事件,並在電子通過新EventArgs的按鈕的名稱,它應該是這樣的Job_JobCompleted( BTN,新的EventArgs())

0

與代碼

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看起來像,但它可能有一個構造函數...