1

你好朋友我正在开发一些在 vb.net 中制作的会计应用程序。所以我正在从我的应用程序发出网络请求,但它阻止了我的应用程序,直到它从网络得到响应,所以我决定使用线程。但我需要多次发出网络请求,所以当我再次启动线程时,它只会让我的应用程序没有响应并关闭。我也尝试在整个互联网上搜索,但没有与我的解决方案相匹配。我也尝试过thread.suspend()、thread.abort()、thread.resume()、.....我的代码示例。

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

当它第二次尝试时,它会使我的应用程序崩溃。我的示例代码如下所示,请有人帮帮我。

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
4

2 回答 2

1

这是您可以无限期地继续创建新线程的方法:

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

请注意,如果不受控制,此循环将很快耗尽内存,并且您的应用程序可能会崩溃。

于 2012-12-10T00:18:54.047 回答
0

您将需要重新设计它。首先在 checkbox.checkchanged 上发送短信是个坏主意,用户可能会在完成之前多次选中和取消选中,提交按钮或类似按钮会更好。

接下来从线程访问 ui 元素是一个坏主意,Form5 类对我来说当然听起来像一个 ui 元素,还有 TextBoxX17。

如果没有看到你的代码的其余部分,它实际上不可能给出一个具体的答案,但我建议创建一个类来发送短信,这会启动一项任务。如果您需要对此任务的反馈,请提出一个事件并在您的主表单上订阅它。

于 2012-12-10T06:56:41.010 回答