Question Weather Software Problem

aaweatherbug

New member
Joined
Jul 14, 2015
Messages
1
Programming Experience
1-3
I am making a weather program and I need some help with it. I am trying to make it to where when the local forecast prompts. The backgroundworker starts (got that done) but I want the SAPI voice to only go when specific tabs are open. This is my code that I have put in the backgroundworker. Visual basic said its "Not a safe way to do this" or something around those terms.
    Private Sub VocalLocal_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles VocalLocal.DoWork
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")
        If TabControl1.SelectedTab Is TabPage1 Then
            (SAPI.Speak("Currently the temperature is")
            SAPI.Speak(Label4.Text)
            Dim v As String = Label2.Text.ToLower
            Select Case True
                Case v = "Thunderstorms" : SAPI.Speak("With a Thunderstorm.")
                Case v = "mixed rain and snow" : SAPI.Speak("With a wintery mix.")
                Case v = "mixed rain and sleet" : SAPI.Speak("With a wintery mix.")
                Case v = "mixed snow and sleet" : SAPI.Speak("With a wintery mix.")
                Case v = "freezing drizzle" : SAPI.Speak("With freezing drizzle")
                Case v = "drizzle" : SAPI.Speak("With drizzle.")
                Case v = "freezing rain" : SAPI.Speak("With freezing rain")
                Case v = "showers" : SAPI.Speak("With showers.")
                Case v = "snow flurries" : SAPI.Speak("With flurries.")
                Case v = "light snow showers" : SAPI.Speak("With snow showers.")
                Case v = "blowing snow" : SAPI.Speak("With blowing snow.")
                Case v = "snow" : SAPI.Speak("With snow.")
                Case v = "sleet" : SAPI.Speak("With sleet.")
                Case v = "dust" : SAPI.Speak("Under dusty conditions.")
                Case v = "foggy" : SAPI.Speak("Under foggy conditions.")
                Case v = "haze" : SAPI.Speak("Under hazy conditions.")
                Case v = "smoky" : SAPI.Speak("Under smoky conditions.")
                Case v = "blustery" : SAPI.Speak("With blustery conditions.")
                Case v = "windy" : SAPI.Speak("With winy conditions.")
                Case v = "cloudy" : SAPI.Speak("Under cloudy skies.")
                Case v = "Mostly Cloudy" : SAPI.Speak("Under mostly cloudy skies.")
                Case v = "Partly Cloudy" : SAPI.Speak("Under partly cloudy skies.")
                Case v = "clear" : SAPI.Speak("Under clear skies.")
                Case v = "sunny" : SAPI.Speak("Under sunny skies.")
                Case v = "fair" : SAPI.Speak("With fair skies.")
                Case v = "hot" : SAPI.speak("With hot conditions.")
                Case v = "heavy snow" : SAPI.Speak("With heavy snow.")
                Case v = "light rain" : SAPI.Speak("With light rain.")
                Case v = "rain" : SAPI.Speak("With rainy conditions.")
                Case v = "Thunder" : SAPI.Speak("With a thunderstorm in the area."))
            End Select
        End If
        If TabControl1.SelectedTab Is TabPage4 Then
            SAPI.Speak(Label12.Text)
        End If
        If TabControl1.SelectedTab Is TabPage3 Then
            SAPI.Speak("Here is your local doppler radar. Showing any precipitation in the region.")
        End If
        If TabControl1.SelectedTab Is TabPage2 Then
            SAPI.Speak(Label14.Text)
            SAPI.Speak("Stay tuned for your accurate and dependable forecasts right here on the A W L Weather Network: The Weather Station You Can Always Turn Too!")
        End If
    End Sub

I use the Yahoo Weather API for this program. Some of the information I type up and it goes directly to the program.

This is the error I get from Visual Basic. "System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Cross-thread operation not valid: Control 'TabControl1' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
at System.Windows.Forms.TabControl.get_SelectedIndex()
at System.Windows.Forms.TabControl.get_SelectedTabInternal()
at System.Windows.Forms.TabControl.get_SelectedTab()
at NWIDC___Lightning_Edition.LocalForecast.VocalLocal_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\Austin\Documents\Visual Studio 2010\Projects\NWIDC - Lightning Edition\NWIDC - Lightning Edition\LocalForecast.vb:line 12
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:"
Can anyone tell me the code to make it run right? Thanks!
 
Last edited:

Similar threads

Back
Top