Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing File in a thread
    primarykey
    data
    text
    <p>I'm working on a xml service at the moment , which is a sum of 20+ other xml's from other site's services. So at first it was just ; </p> <pre><code>GetherDataAndCreateXML(); </code></pre> <p>But obviously getting 20+ other xml , editing and serving it takes time , so i decided to cache it for like 10 minutes and added a final.xml file with a DateTime attribute to check if it's out of date etc. So it became something like ;</p> <pre><code>var de = DateTime.Parse(x.Element("root").Attribute("DateTime").Value).AddSeconds(10.0d); if (de &gt;= DateTime.Now) return finalXML(); else { RefreshFinalXml(); return finalXML(); } </code></pre> <p>The problem now , is that any request after that 10 minute obviously takes too much time as it's waiting for my looong RefreshFinalXml() function. So i did this;</p> <pre><code>if (ndt &gt;= DateTime.Now) return finalXML(); else { ThreadStart start = RefreshFinalXml; var thr = new Thread(start); thr.IsBackground = true; thr.Start(); return finalXML(); } </code></pre> <p>This way , even at the 11th minute i simply return the old final.xml but meanwhile i start another thread to refresh current xml at the background. So after something like 13th minute , users get fresh data without any delay. But still there is a problem with this ; it creates a new thread for every single request between 10 to 13th minutes ( while first RefreshFinalXml is still working at the background ) and obviously i can't let that happen , right? And since I don't know much about locking files and detecting if it's lock , i added a little attribute , "Updating" to my final xml ;</p> <pre><code>if (ndt &gt;= DateTime.Now) return finalXML(); else { if (final.Element("root").Attribute("Updating").Value != "True") { final.Element("root").SetAttributeValue("Updating", "True"); final.Save(Path); ThreadStart start = RefreshFinalXml; //I change Updating Attribute back to False at the end of this function , right before saving Final Xml var thr = new Thread(start); thr.IsBackground = true; thr.Start(); } return finalXML(); } </code></pre> <p>So , 0-10 minutes = return from cache<br> 10~13 minutes = return from cache while just one thread is refreshing final.xml 13+ minutes = returns from cache </p> <p>It works and seems decent at the moment , but the question/problem is ; I'm extremely inexperienced in these kind of stuff ( xml services , threading , locks etc ) so i'm not really sure if it'll work flawlessly under tougher situations. For example , will my custom locking create problems under heavy traffic, should i switch to lock file etc.</p> <p>So I'm looking for any advice/correction about this process , what would be the "best practice" etc.</p> <p>Thanks in advance<br> Full Code : <a href="http://pastebin.com/UH94S8t6" rel="nofollow">http://pastebin.com/UH94S8t6</a></p> <p>Also apologies for my English as it's not my mother language and it gets even worse when I'm extremely sleepless/tired as I'm at the moment.</p> <p><strong>EDIT</strong> : Oh I'm really sorry but somehow i forgot to mention a crucial thing ; this is all working on Asp.Net Mvc2. I think i could have done a little better if it wasn't a web application but i think that changes many things right?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload