Rahul Singh
Rahul Singh

Reputation: 11

Run same thread again in vb.net

Hello friend i am working on some accounting application made in vb.net. So i am making web request from my application but it block my application till it get respond from web so i decided to use thread. but i need to make web request multiple times so when ever i start thread again it just make my application not responding and it close. I have also try searching all over internet but nothing match to my solution. I have also try thread.suspend(), thread.abort(), thread.resume(), ..... sample of my code.

Dim MyThread As System.Threading.Thread

Private Sub CheckBoxX1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxX1.CheckedChanged

  If CheckBoxX1.Checked = True Then
        Try
            MyThread = New System.Threading.Thread(AddressOf sendsms)
            MyThread.Start()
        Catch ex As Exception
        msgbox(ex.message)
        End Try
   End If
End Sub

Private Sub sendsms()

    Dim settings As New Form5

    password = New System.Text.ASCIIEncoding().GetString(Convert.FromBase64String(password))

    Dim smsapi As String = Nothing

    smsapi = ini.GetString("Setting_SMS", "api", smsapi)

    settings.sendSMS(TextBoxX17.Text, msg, provider, username, password, smsapi)

    MyThread.Suspend()

End Sub

When it try 2nd time it crash my Application. My sample code is give below can some one please help me out.

Public Class Form2
    Private Sub ButtonX4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click
        Dim ch As New System.Threading.Thread(AddressOf sendsms)
        ch.IsBackground = True
        ch.Start()
    End Sub

    Private Sub sendsms()
        msg = msg.Replace("{name}", TextBoxX18.Text)
        msg = msg.Replace("{totalprice}", TextBoxX4.Text)
        msg = msg.Replace("{id}", TextBoxX21.Text)
        msg = msg.Replace("{vehicalnum}", TextBoxX15.Text)
        password = New System.Text.ASCIIEncoding().GetString(Convert.FromBase64String(password))
        Dim smsapi As String = Nothing
        smsapi = ini.GetString("Setting_SMS", "api", smsapi)
        Dim settings As New sendsms
        settings.sendSMS(TextBoxX17.Text, msg, provider, username, password, smsapi)
    End Sub

End Class



Public Class sendsms
    Function sendSMS(ByVal numbers As String, ByVal msg As String, ByVal provider As String, ByVal username As String, ByVal password As String, ByVal api As String)
        Try
            If api = "" Then
                api = "http://ultimatesmsapi.tk/sms.php?provider=provider1&username=username1&password=password1&numbers=numbers1&msg=msg1"
            End If

            api = api.Replace("username1", username)
            api = api.Replace("password1", password)
            api = api.Replace("msg1", msg)
            api = api.Replace("numbers1", numbers)
            api = api.Replace("provider1", provider)
            Dim myReq As HttpWebRequest
            myReq = DirectCast(WebRequest.Create(api), HttpWebRequest)
            Dim myResp As HttpWebResponse = DirectCast(myReq.GetResponse(), HttpWebResponse)
            Dim respStreamReader As New System.IO.StreamReader(myResp.GetResponseStream())
            Dim responseString As String = respStreamReader.ReadToEnd()
            If responseString = 1 Then
                msg = "SMS sent successfully.."
            ElseIf responseString = -1 Then
                msg = "Username and password is incorrect"
            ElseIf responseString = -2 Then
                msg = "Some fields are missing!"
            ElseIf responseString = -3 Then
                msg = "Error while sending sms!"
            ElseIf responseString = -4 Then
                msg = "Invalid Provider!"
            ElseIf responseString = -5 Then
                msg = "Number is DND activated!"
            End If
            respStreamReader.Close()
            myResp.Close()
        Catch ex As Exception
            msg = "Problem in connection to server.."
        End Try
        MsgBox(msg)
        Return msg
    End Function
End Class

Upvotes: 1

Views: 2663

Answers (2)

Steve
Steve

Reputation: 20469

You are going to need to redesign this. First sending an sms on checkbox.checkchanged is a bad idea, the user is likely to check and uncheck multiple times before they are done, a submit button or similar would be better.

Next accessing ui elements from a thread is a bad idea, and the class Form5 certainly sounds like a ui element to me, along with TextBoxX17.

Without seeing the rest of your code its not really possible to give a specific answer, but i would suggest creating a class to send the sms, that starts a task. If you need feedback from this task, raise an event and subscribe to it on your main form.

Upvotes: 0

Victor Zakharov
Victor Zakharov

Reputation: 26414

This is how you can indefinitely keep creating new threads:

Do
    Dim MyThread As New System.Threading.Thread(AddressOf sendsms)
    MyThread.Start()
Loop

Please note this loop will soon run out of memory, if uncontrolled, and you application may crash.

Upvotes: 1

Related Questions