none
Stop program while running code RRS feed

  • Question

  • I have code to fetch part of the weather from the Yahoo site
    The code works well
    However, when you press the mouse repeatedly on the front window, the waiting window appears or exit the program as a whole

    What I ask is
    When you press the button no matter how often Windows does not appear to wait

    Saturday, April 7, 2018 6:25 PM

Answers

  •  It only takes about 2 or 3 seconds to load the xml file and display the info on my end.  It is the loading of the file that takes 2 or 3 seconds and the displaying data part is almost instant so,  without knowing how this fits into your app,  perhaps you can just load the xml document when your app starts and just display the info when needed.  It will display the data pretty much instantly.

     Perhaps you may still want to run the code in a separate thread so that your app stays responsive.

    Imports System.Threading
    Imports System.Xml
    
    Public Class Form1
        Private WeatherThread As Thread
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If WeatherThread Is Nothing OrElse Not WeatherThread.IsAlive Then
                WeatherThread = New Thread(AddressOf GetWeatherInfo)
                WeatherThread.Start()
            End If
        End Sub
    
        Private Sub GetWeatherInfo()
            Try
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
    
                Dim xmlnode As XmlNodeList = xmlDoc.GetElementsByTagName("title")
                Dim title As String = xmlnode(2).InnerText
    
                Dim publishdate As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("pubDate")
                    publishdate = node.InnerText
                Next
    
                Dim text As String = ""
                Dim date1 As String = ""
                Dim day As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("yweather:forecast")
                    text = node.Attributes("text").InnerText
                    date1 = node.Attributes("date").InnerText
                    day = node.Attributes("day").InnerText
                Next
    
                Me.Invoke(Sub()
                              TextBox1.Text = title
                              TextBox2.Text = publishdate
                              TextBox3.Text = text
                              TextBox4.Text = date1
                              TextBox5.Text = day
                          End Sub)
    
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Sub
    End Class
    
     

     This is using the thread to keep the app responsive...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, April 7, 2018 9:47 PM
    • Marked as answer by monemas Sunday, April 8, 2018 12:02 AM
    Saturday, April 7, 2018 9:25 PM

All replies

  • Hi

    Such little information! It may be that the application is busy doing something. Do you have any idea what your application is doing?

    Probably/possibly in a loop somewhere doing something and can't respond to the Button.

    Maybe you have an exception being thrown from within Catch block within a Try Catch block.

    Maybe the application is trying to get data from the web site and is busy waiting on it.

    Maybe ........lots of maybes' ..................


    Regards Les, Livingston, Scotland

    Saturday, April 7, 2018 6:34 PM
  • Hi

    Such little information! It may be that the application is busy doing something. Do you have any idea what your application is doing?

    Probably/possibly in a loop somewhere doing something and can't respond to the Button.

    Maybe you have an exception being thrown from within Catch block within a Try Catch block.

    Maybe the application is trying to get data from the web site and is busy waiting on it.

    Maybe ........lots of maybes' ..................


    Regards Les, Livingston, Scotland


    Is there a way for this message not to appear?


    Or your words mean that something is compulsory
    • Edited by monemas Saturday, April 7, 2018 6:59 PM
    Saturday, April 7, 2018 6:58 PM
  • Hi

    You have shown NO code - do you really think I (we) can read minds?

    Put some breakpoints and run to each until the programme hangs, then delete all breakpoint except for the one before the hang, run to it and single step from there until computer hangs and you should be in the area that needs to be checked.


    Regards Les, Livingston, Scotland

    Saturday, April 7, 2018 7:16 PM
  • Hi

    You have shown NO code - do you really think I (we) can read minds?

    Put some breakpoints and run to each until the programme hangs, then delete all breakpoint except for the one before the hang, run to it and single step from there until computer hangs and you should be in the area that needs to be checked.


    Regards Les, Livingston, Scotland

    aim sorry

    full code

     Try
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
    
                Dim xmlnode As XmlNodeList
    
                xmlnode = xmlDoc.GetElementsByTagName("title")
                Dim title = xmlnode(2).InnerText
                textbox1.text = title
                xmlnode = xmlDoc.GetElementsByTagName("pubDate")
                For Each node As XmlNode In xmlnode
                    Dim pudate = node.InnerText
                    textbox2.text = pudate
                Next
    
                xmlnode = xmlDoc.GetElementsByTagName("yweather:forecast")
                For Each node As XmlNode In xmlnode
                    Dim text = node.Attributes("text").InnerText
                    Dim date1 = node.Attributes("date").InnerText
                    Dim day = node.Attributes("day").InnerText
    
                    textbox3.text = text : textbox4.text = date1 : textbox5.text = day
                Next
            Catch ex As Exception
    
            End Try

    Saturday, April 7, 2018 7:41 PM
  • The thing you need to do is to disable the button so that you can't click on it anymore until program finishes its functionality, then you can enable the button again.

     
    Saturday, April 7, 2018 8:03 PM
  • Coupled with DA924x suggestion you might want to flush any clicks when the button is disabled.

    http://www.vb-helper.com/howto_net_flush_clicks.html


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 7, 2018 8:20 PM
    Moderator
  • Hi

    The very least you must do, is to put a STOP in the Catch block, or, better still, a MessageBox.Show(ex.message)

    BTW: I tried your code and had no issues!

    Regards Les, Livingston, Scotland

    Saturday, April 7, 2018 8:53 PM
  •  It only takes about 2 or 3 seconds to load the xml file and display the info on my end.  It is the loading of the file that takes 2 or 3 seconds and the displaying data part is almost instant so,  without knowing how this fits into your app,  perhaps you can just load the xml document when your app starts and just display the info when needed.  It will display the data pretty much instantly.

     Perhaps you may still want to run the code in a separate thread so that your app stays responsive.

    Imports System.Threading
    Imports System.Xml
    
    Public Class Form1
        Private WeatherThread As Thread
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If WeatherThread Is Nothing OrElse Not WeatherThread.IsAlive Then
                WeatherThread = New Thread(AddressOf GetWeatherInfo)
                WeatherThread.Start()
            End If
        End Sub
    
        Private Sub GetWeatherInfo()
            Try
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
    
                Dim xmlnode As XmlNodeList = xmlDoc.GetElementsByTagName("title")
                Dim title As String = xmlnode(2).InnerText
    
                Dim publishdate As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("pubDate")
                    publishdate = node.InnerText
                Next
    
                Dim text As String = ""
                Dim date1 As String = ""
                Dim day As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("yweather:forecast")
                    text = node.Attributes("text").InnerText
                    date1 = node.Attributes("date").InnerText
                    day = node.Attributes("day").InnerText
                Next
    
                Me.Invoke(Sub()
                              TextBox1.Text = title
                              TextBox2.Text = publishdate
                              TextBox3.Text = text
                              TextBox4.Text = date1
                              TextBox5.Text = day
                          End Sub)
    
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Sub
    End Class
    
     

     This is using the thread to keep the app responsive...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, April 7, 2018 9:47 PM
    • Marked as answer by monemas Sunday, April 8, 2018 12:02 AM
    Saturday, April 7, 2018 9:25 PM
  •  It only takes about 2 or 3 seconds to load the xml file and display the info on my end.  It is the loading of the file that takes 2 or 3 seconds and the displaying data part is almost instant so,  without knowing how this fits into your app,  perhaps you can just load the xml document when your app starts and just display the info when needed.  It will display the data pretty much instantly.

     Perhaps you may still want to run the code in a separate thread so that your app stays responsive.

    Imports System.Threading
    Imports System.Xml
    
    Public Class Form1
        Private WeatherThread As Thread
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If WeatherThread Is Nothing OrElse Not WeatherThread.IsAlive Then
                WeatherThread = New Thread(AddressOf GetWeatherInfo)
                WeatherThread.Start()
            End If
        End Sub
    
        Private Sub GetWeatherInfo()
            Try
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
    
                Dim xmlnode As XmlNodeList = xmlDoc.GetElementsByTagName("title")
                Dim title As String = xmlnode(2).InnerText
    
                Dim publishdate As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("pubDate")
                    publishdate = node.InnerText
                Next
    
                Dim text As String = ""
                Dim date1 As String = ""
                Dim day As String = ""
                For Each node As XmlNode In xmlDoc.GetElementsByTagName("yweather:forecast")
                    text = node.Attributes("text").InnerText
                    date1 = node.Attributes("date").InnerText
                    day = node.Attributes("day").InnerText
                Next
    
                Me.Invoke(Sub()
                              TextBox1.Text = title
                              TextBox2.Text = publishdate
                              TextBox3.Text = text
                              TextBox4.Text = date1
                              TextBox5.Text = day
                          End Sub)
    
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Sub
    End Class
    
     

     This is using the thread to keep the app responsive...


    If you say it can`t be done then i`ll try it

    I am very happy with your answer to this question
    It will solve many problems

    Sunday, April 8, 2018 12:27 AM