你好朋友我正在开发一些在 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