c# - WebClient() never reaching DownloadStringCompleted on WPF application -


so i'm confused, why webclient not accessing downloadstringcompleted. after reading possible problems webclient being disposed, before can finish download. or exceptions not being caught during downloaddata or uri inaccessible.

i've checked against these problems, , webclient has not yet accessed downloadstringcompleted.

pmid webclient class

    /// <summary>     /// construct new curl     /// </summary>     /// <param name="pmid">pmid value</param>     public pmidcurl(int pmid)     {         this.pmid = pmid;         stringbuilder pmid_url_string = new stringbuilder();         pmid_url_string.append("http://www.ncbi.nlm.nih.gov/pubmed/").append(pmid.tostring()).append("?report=xml");         this.pmid_url = new uri(pmid_url_string.tostring());     }     /// <summary>     /// curl data pmid     /// </summary>     public void curlpmid()     {         webclient client = new webclient();         client.downloadstringcompleted += new downloadstringcompletedeventhandler(httpscompleted);         client.downloaddata(this.pmid_url);     }     /// <summary>     /// information store in class after curl     /// </summary>     public string abstracttitle { get; set; }     public string abstracttext { get; set; }     /// <summary>     /// retrieve data xml file pmid     /// </summary>     /// <param name="sender">system generated</param>     /// <param name="e">system generated</param>     private void httpscompleted(object sender, downloadstringcompletedeventargs e)     {         if (e.error == null)         {             pmidcrawler pmc = new pmidcrawler(e.result, "/pre/pubmedarticle/medlinecitation/article");             //iterate on each node in file             foreach (xmlnode xmlnode in pmc.crawl)             {                 this.abstracttitle = xmlnode["articletitle"].innertext;                 this.abstracttext = xmlnode["abstract"]["abstracttext"].innertext;             }         }     } //close httpscompleted 

pmid nodelist constructor class

    /// <summary>     /// list initialized crawer     /// </summary>     public xmlnodelist crawl { get; set; }      /// <summary>     /// constructor html xml converter     /// </summary>     /// <param name="nhtml"></param>     /// <param name="nodelist"></param>     public pmidcrawler(string nhtml, string nodelist)     {         //parse e         string html = httputility.htmldecode(nhtml);         xdocument htmldoc = xdocument.parse(html, loadoptions.none);         //convert xdocument xmldocument         xmldocument xmldoc = new xmldocument();         xmldoc.load(htmldoc.createreader());         //load xmldocument nodelist         xmlelement xmlroot = xmldoc.documentelement;         this.crawl = xmlroot.selectnodes(nodelist);     } 

any ideas on why donwloadstringcompleted never reached?

you have several issues curlpmid code. have put comments in code below.

public void curlpmid() {     // 1. variable 'client' loses scope when function exits.     //    may want consider making class variable, doesn't     //    disposed early.     webclient client = new webclient();     client.downloadstringcompleted += new downloadstringcompletedeventhandler(httpscompleted);     // 2. calling synchronous version of download function.     //    synchronous version not call completion handlers.     //    when synchronous call returns, download has completed.     // 3. calling wrong function here.  based on completion handler,      //    should calling downloadstringasync(). if want synchronous      //    behavior, call downloadstring() instead.     client.downloaddata(this.pmid_url); } 

in short, assuming want async behavior, curlpmid function should like:

public void curlpmid() {     webclient client = new webclient();     client.downloadstringcompleted += new downloadstringcompletedeventhandler(httpscompleted);     client.downloadstringasync(this.pmid_url); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -