您好朋友我正在研究vb.net中的一些会计应用程序。所以我从我的应用程序发出web请求,但它阻止了我的应用程序,直到它得到网络响应,所以我决定使用线程。但我需要多次发出Web请求,所以当我再次启动线程时,它只是使我的应用程序没有响应,它关闭。我也尝试在互联网上搜索,但没有与我的解决方案匹配。我也尝试了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
答案 0 :(得分:1)
这是你可以无限期地继续创建新线程的方法:
Do
Dim MyThread As New System.Threading.Thread(AddressOf sendsms)
MyThread.Start()
Loop
请注意,如果不受控制,此循环将很快耗尽内存,并且您的应用程序可能会崩溃。
答案 1 :(得分:0)
你需要重新设计它。首先在checkbox.checkchanged上发送短信是一个坏主意,用户可能会在完成之前多次检查和取消选中,提交按钮或类似内容会更好。
接下来从线程中访问ui元素是一个坏主意,而Form5对我来说肯定听起来像是一个ui元素,还有TextBoxX17。
如果没有看到你的其余代码,那么它不可能给出一个特定的答案,但我会建议创建一个发送短信的类来启动任务。如果您需要此任务的反馈,请举办活动并在主表单上订阅。