Jitnaught Posted June 24, 2014 Share Posted June 24, 2014 (edited) (Title is supposed to be "Get XML from web without lag/pause".) Hi everyone. I'm trying to create a script which gets the current weather condition in your specified city, but I've got a problem. The game lags/pauses when I'm getting the XML. When I say pause, I don't mean like the pause menu, I mean the game stops doing anything until the script receives the XML. Here is my function that I have now: public static string GetCurrentConditions(string location) { try { string condition = "No data"; XmlReader xmlReader = XmlReader.Create(string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml", location)); while (xmlReader.Read()) { if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "weather") && xmlReader.HasAttributes) { condition = xmlReader.GetAttribute("number"); break; } } return condition; } catch (Exception ex) { return ex.ToString(); } } It works perfectly, but has the lag, which is annoying and I wouldn't want to release a mod that does that. I looked up the fastest way and I found that XmlReader was the fastest (I was using XmlDocument before). I don't think the parsing of the file is the cause of the lag because the XML is a small file. Just look at this. Is there any other way to get the XML without this lag/pause? P.S. I have never done anything related to XML, so if you think I sound like a noob or something, this is why. Edited June 24, 2014 by LetsPlayOrDy Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 24, 2014 Share Posted June 24, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Wiebrendh 1 Link to comment Share on other sites More sharing options...
Jitnaught Posted June 24, 2014 Author Share Posted June 24, 2014 you need to get the weather in a new System.Threading.Thread and then when that is finished... it can set some stringvariable and maybe trigger a bool to let you know the weather is ready for your script to use... using System.Threading;...string weatherInfo = "Retrieving weather...please wait...";//you can be drawing this as text in a per frame draw event while the thread is runningThread t = new Thread(new ThreadStart(() =>{ //get weather here!! //dont write any gta related code in here like calling natves weatherInfo = yourParsedAndFormattedString;}));t.IsBackground = true;//this thread running will not prevent the app gtaiv.exe from closingt.Start(); Thanks! I tried it and it seems to be working. I'll post if otherwise Wiebrendh 1 Link to comment Share on other sites More sharing options...
InfamousSabre Posted June 24, 2014 Share Posted June 24, 2014 Ha! had the same idea as me I just finished mine a few days ago. used wunderground API Wont be releasing though. GTA IV's weather just isn't dynamic enough for me to be pleased with the result. Hope yours goes well Link to comment Share on other sites More sharing options...
Jitnaught Posted June 25, 2014 Author Share Posted June 25, 2014 Ha! had the same idea as me I just finished mine a few days ago. used wunderground API Wont be releasing though. GTA IV's weather just isn't dynamic enough for me to be pleased with the result. Hope yours goes well Yeah, I know. I wish it was more dynamic too I'm still going to release mine though Link to comment Share on other sites More sharing options...
InfamousSabre Posted June 25, 2014 Share Posted June 25, 2014 in .net4+ you can also do this instead of creating a new thread System.Threading.Tasks.Task.Factory.StartNew(() =>{ //get weather here!! //dont write any gta related code in here like calling natves weatherInfo = yourParsedAndFormattedString;})); Haven't personally compared it against a new thread, but have read that its supposed to be less taxing on cpu/ram*shrug* give it a shot if you'd like. Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 25, 2014 Share Posted June 25, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Link to comment Share on other sites More sharing options...
NTAuthority Posted June 26, 2014 Share Posted June 26, 2014 actually the thread pool is primarily meant for short-lived work objects; longer-term tasks are still better off on a manually-managed thread or passing a specific flag to the native thread pool functions that doesn't happen to be exposed in the BCL/FCL. LordOfTheBongs 1 Inactive in GTA/R* title modification indefinitely pursuant to a court order obtained by TTWO. Good job acting against modding! Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 26, 2014 Share Posted June 26, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Link to comment Share on other sites More sharing options...
Jitnaught Posted June 26, 2014 Author Share Posted June 26, 2014 oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic. Ive parsed huge web pages just using string methods and it is always super quick... getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does I already basically knew it isn't the lag. I mentioned that in my post Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 27, 2014 Share Posted June 27, 2014 oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic. Ive parsed huge web pages just using string methods and it is always super quick... getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does I already basically knew it isn't the lag. I mentioned that in my post u sounded unsure so i thought id tell u what i experienced before Link to comment Share on other sites More sharing options...
Jitnaught Posted June 27, 2014 Author Share Posted June 27, 2014 oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic. Ive parsed huge web pages just using string methods and it is always super quick... getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does I already basically knew it isn't the lag. I mentioned that in my post u sounded unsure so i thought id tell u what i experienced before Ah ok Link to comment Share on other sites More sharing options...
Jitnaught Posted June 27, 2014 Author Share Posted June 27, 2014 When I use a thread and put a loop in it to keep checking for the weather, it freezes the game when the game starts. Does anyone know how to fix this? If you need my code just tell me. Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 27, 2014 Share Posted June 27, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Link to comment Share on other sites More sharing options...
Jitnaught Posted June 27, 2014 Author Share Posted June 27, 2014 ^ Don't know if you meant you wanted to look through the code or not, but I'll post it anyways. This is the code I have right now. It runs when the script starts. Thread thread = new Thread(new ThreadStart(() => { while (!done) { if (rwEnabled) { string getCondition = GetCurrentConditions(sLocation); if (currentCondition != getCondition) { currentCondition = getCondition; Game.Console.Print("Real Weather: Updated weather: " + CurrentConditionToName(getCondition)); } Thread.Sleep(60000); } } })); thread.IsBackground = true; thread.Start(); GetCurrentConditions gets the weather XML then parses it to get the condition (which is a string). CurrentConditionToName converts the current condition to a name (like "thunderstorm" or "drizzle"). I don't think those are what are causing the freezing, as before I added the thread these functions worked fine. Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 27, 2014 Share Posted June 27, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Link to comment Share on other sites More sharing options...
Jitnaught Posted June 27, 2014 Author Share Posted June 27, 2014 see what happens if u comment out the printing to the console, it needs to access the console object and this object i guess needs to be serializable to pass between threads... im not 100% certain though, just from my basic understanding of threading Well that worked. Thanks Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 27, 2014 Share Posted June 27, 2014 (edited) ... Edited July 2, 2014 by LordOfTheBongs Link to comment Share on other sites More sharing options...
Jitnaught Posted June 27, 2014 Author Share Posted June 27, 2014 ok cool, yeah i think he needed to add a Serializable attribute to the console class to make it be able to be serialized and rebuilt in the new thread so it could be used in your thread... anyways, just use a bool or whatever u like to trigger the code in your actual script running on the script thread Already did LordOfTheBongs 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now