c# - How do I display query results in the same order they were made in a multithreaded web app? -


i asked here on using multithreading in simple asp.net windows forms applet make searches run faster, , got answers. result of using threading on app (and not having experience it), i've run number of new design problems.

i've written simple applet sends post request third party website use search bar, , return integer extracted string on page indicates how many results found given search term.

public partial class _default : system.web.ui.page {     private nlsearch search;     private static list<searchresult> resultslist = new list<searchresult>();       protected void page_load(object sender, eventargs e)     {         search = new nlsearch();     }      protected void addsearchmethod(object sender, eventargs e)     {         var text = searchform.text;         task task = task.factory.startnew(() => makerequest(text));         task.wait();          resultslabel.text = "";         foreach (var v in resultslist)         {             resultslabel.text += v.searchterm + ": " + v.count + " occurances<br/>";         }     }      protected void clearsearchmethod(object sender, eventargs e)     {         resultslabel.text = "";         resultslist.clear();     }      protected void makerequest(string text)     {         searchresult s = new searchresult         {             searchterm = text,             count = 0         };         resultslist.add(s);         s.count = search.makerequests(text);     } } 

addsearchmethod , clearsearchmethod correspond "add search" , "clear search" button, , interact searchform textfield , resultslabel label.

the desired behavior is:

  1. that client can enter single search term in searchform field, submit, , application run search, add result resultslist list, , write contents of list resultslabel, works intended.

  2. i client able submit many search terms searchform, , write list in order made. response http request quite slow , improves usability.

i run lot of issues #2. can submit many search terms want before receive response, response extremely slow , seems scale number of requests, suggesting not running concurrently. fails preserve order, if enter search terms in order "1990" "1991" 1992" "1993", display in order response received, rather order made.

not knowing threading web apps, i'm not sure how troubleshoot these issues.

edit: there seems loss of requests. example if enter in order "boston1" through "boston10" output

boston1: 302 occurances boston3: 265 occurances boston2: 227 occurances boston: 10220056 occurances boston7: 389 occurances boston10: 109 occurances 

just use sorted list , keep track of requests using static request id number.

private static sortedlist<long, searchresult> resultslist = new sortedlist<long, searchresult>(); ...     foreach (var v in resultslist.values)     { ... public static long requestid = 0; protected void makerequest(string text) {     searchresult s = new searchresult     {         searchterm = text,         count = 0     };     resultslist.add(system.threading.interlocked.increment(ref requestid), s); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -