My app freezes due to internet connection. How to solve this?
I start my app using the Sub_Main() in a module, and from there it launches the first form called frmBackImg.
On the form_load event of frmBackImg it loads a setting (using "GetSetting" ) and it detects whether it is the first time the user runs this app or not.
If it is not the first time, it loads a form called frmWeather (after executing some other lines of codes needed in the form_load event ofc ).
Meanwhile, this is what is going on in frmWeather:
There are two DOM documents referenced to MSXML v. 3 and one checks a XML file for a node, while the other checks the same XML file for another node's attributes.
There are two error checks: one is to check whether there was any error while loading the XML target and the other one checks whether there was any error with parsing the XML. (thanks to a user here on the forums who helped me. Btw, all the names of those that really helped me here, will be in my app )
I am using the Personal Hotspot feature on my iPhone in order to connect my PC to the internet using my phone's network. But I have no credits on my phone, so the internet cannot be accessed although the PC is connected. So, when I launch my app, it freezes while executing the two error checks I think.
When I hover the mouse on the form, it turns to an hourglass, and if I click the form it freezes and Windows asks me to end the program or wait. I tried using DoEvents but it didn't work. I even added DoEvents after every line of the frmWeather_load events and it still freezes. When I am not connected to the internet the app doesn't freeze and it just shows that there is no internet connection so the Weather is not available. How can I solve this? Should I use the inet control to check whether the user is connected to the internet or not? Any suggestions? Thanks in advance
Re: My app freezes due to internet connection. How to solve this?
I think the easiest fix is to make async requests and use some timer (Timer control or whatever) to time out requests and abort them. See ThinDavApi, Cloud Storage for the Masses for an example of doing just this.
I know this will frustrate you because you can't just write inline synchronous code (always a poor idea in VB anyway), but between connectivity problems and network latency you risk this kind of lockup.
Re: My app freezes due to internet connection. How to solve this?
Since you said to make async requests, I set the async property of the DOM to true, but now it just pops up an error message saying that the data is not available yet. I will look into that API right now. Damn it! Why is it that every time I fix a small bug, another bigger one pops up!
Maybe I could reference the problem this way:
The frmBackImage is just a rounded form with a background image and some opacity. I tried to divide my app into layers (forms). The first one is the background, the second one is the Weather function. Whenever the user double clicks the form, it comes down from the top (like it is falling or sth) and after falling down, it loads the frmWeather form. This frmWeather form is supposed to be on top of the frmBackImage. But after calling the frmWeather form, it takes a few seconds for the form to show (since it is busy working with XML) so there is only the frmBckImg which is wrong, because when the user double clicks the frmBackImg, as it comes down, the frmWeather should do that too. That's why I loaded the frmWeather while loading the frmBackImg, before the user has the possibility to double click and when he does, everything will be there. But this just made it worse. Maybe you can help me with this one?
EDIT: dilettante, thank you so much for such a quick reply. I gave a look to that API and that is just way too much for what I am trying to do. I was thinking more of a simpler solution. Btw, that API had some great functions. It must have taken a lot of time to create it.
Last edited by kevinKZzaka; Aug 17th, 2013 at 07:05 PM.
Re: My app freezes due to internet connection. How to solve this?
You can't just use async = True, you also have to set up a COM callback for completion before trying to get any data out of the XMLHTTP object. My ThinDavApi class won't help you accomplish what you are doing, but it is example of how to do this sort of request without blocking synchronous calls.
I'm not sure about your "form on a form" issue. Is your problem there because you try to do a synchronous HTTP request during Form_Load or something?
Re: My app freezes due to internet connection. How to solve this?
And how exactly can I do that? Any example would be appreciated.
Yeah, it is really confusing, but it works great (apart from this small internet thing). Is there a way to use a timeout property? For example: the time in which the XML file should be loaded, parsed and analyzed would be about 5 seconds. If the operation is not completed during this time, then there should be like a message telling me that the data could not be loaded and ask whether I want to retry or not.
And if the timeout thing would be possible, I could use the Wait API I've seen in the codebank so that the form could wait about 5 seconds before it shows it self.
UPDATE: I tried this but with no luck:
on the form_load event of frmBackImg where is the code for loading the Weather form, before the line which serves to show the weather form, I added
Code:
me.visible = false
me.hide
on the form_load of the frmWeather is the code for the XML parsing thing. After all that I added
Code:
frmBackimg.show
frmBackImg.visible = true
I tested the compiled app, and as expected the form didn't show until the whole XML processing wasn't done. But that took a looong time. And there is also a form which minimizes itself to the tray and there is a popup menu when the user clicks the tray icon. When I right click the icon, the menu doesn't pop up until the two other forms are done loading. So this solution is out too
Last edited by kevinKZzaka; Aug 17th, 2013 at 07:22 PM.
Re: My app freezes due to internet connection. How to solve this?
Here is a very small example.
What it leaves out is using a timer to call the XMLHTTP object's abort method when too much time has elapsed. But it shows one way to create and set the callback.
Re: My app freezes due to internet connection. How to solve this?
When using a async requests you need to not try to process the document in that same code block and instead wait until the async request signals it has completed.
I take it that when you say PC you mean a laptop computer?
Using a iPhone as the internet connection source for an actual PC would be far less than ideal.
Re: My app freezes due to internet connection. How to solve this?
Thank you for this great example. I tested the project in my PC where I am connected but the internet cannot be accessed and it returned readystate = 1 . What does that mean?
I also tested it on my Windows XP virtual box where there is no network at all, and it returned:
Re: My app freezes due to internet connection. How to solve this?
DataMiser, I use my iPhone's network connection a lot since I have no wifi at home and it works perfectly. And I've tested the app too and it worked as it should. But today I run out of credit (I am still able to access the forum or other sites using a small trick) and when I tested the app after some code changes, this happened and I had no solution for it, no matter how much I tried. And yes, I mean a lap top.
So, I set the DOM's async property to true (this is in the form load event) and then I create a sub in order to do the rest (the XML loading and parsing and analyzing) and then I call this sub from the form_load? Is this what you are saying?
But complete does not mean "all is well" since you have to check the .status property. A .status of 12007 is not an HTTP status code but an XMLHTTP (or WinInet) status/error code, and it is defined as ERROR_INTERNET_NAME_NOT_RESOLVED in the WinInet.h header file.
This means either it could not reach the DNS server or your URL specifies a server that does not exist.
You want a .status = 200 for most GET requests, which means "OK."
Re: My app freezes due to internet connection. How to solve this?
So, can I just use a timer to if-check whether the state equals to 200 in a range of five seconds? And if not, then just pop up a message saying "Failed data loading. Make sure you are connected to the internet and try again"?
Re: My app freezes due to internet connection. How to solve this?
Btw, do I need to make this if-check while the whole XML is being loaded and this would mean that I would have to use a do-while loop. Because if I firstly check whether the state is 200 (and wait about 5 seconds if it is not to recheck again) and then after the state = 200, do all the XML loading and parsing? I think that wouldn't make the app freeze but it still would take a lot of time to display the data.
I still don't get why the app freezes? I mean, if there is any problem with the XML loading or parsing I've added the code to display it, but instead it freezes. Or is it the fact that I am loading a form while another one is still in the process of being completely loaded?
Re: My app freezes due to internet connection. How to solve this?
You'd use the timer to wait for 5 seconds to go by. If the timer fires and completion (readyState = 4) has not happened yet, then call the .abort method. This is your timeout error condition.
If completion occurs before the timer goes off, then stop your timer. Then look for .status = 200, and if so you can retrieve the data. Otherwise you have some kind of error.
You can't just "wait again" because once aborted you need a new request.
Re: My app freezes due to internet connection. How to solve this?
Originally Posted by kevinKZzaka
Btw, do I need to make this if-check while the whole XML is being loaded and this would mean that I would have to use a do-while loop. Because if I firstly check whether the state is 200 (and wait about 5 seconds if it is not to recheck again) and then after the state = 200, do all the XML loading and parsing? I think that wouldn't make the app freeze but it still would take a lot of time to display the data.
When you get the 200 you can load your DOMDocument with the XML. The DOMDocument does all of the parsing, all you are doing is spelunking your way through the resulting DOM (object tree), you are not parsing anything.
This process should be far faster than getting the data over the Internet.
Originally Posted by kevinKZzaka
I still don't get why the app freezes? I mean, if there is any problem with the XML loading or parsing I've added the code to display it, but instead it freezes. Or is it the fact that I am loading a form while another one is still in the process of being completely loaded?
We haven't seen any of your code but you admitted to using a synchronous .Open call. That is where your program was hanging.