我有一个课程,其主要功能是:
public void SendSMS(SendInfo info, WebBrowser browser)
{
browser.Width = 300;
browser.Height = 300;
browser.ScriptErrorsSuppressed = true;
browser.DocumentCompleted += Browser_Navigated;
this.number = info.number;
this.message = info.template;
if (info.proxy != null) { WebRequest.DefaultWebProxy = info.proxy; }
debugCode = Application.OpenForms["Form1"].Controls["tabControl1"].Controls["tabPage1"].Controls["DebugCode"] as TextBox;
debugImage = Application.OpenForms["Form1"].Controls["tabControl1"].Controls["tabPage1"].Controls["pictureBox1"] as PictureBox;
MessageBox.Show("I'am start send, template: " + info.template);
browser.Navigate("My secret url :)");
}
浏览器导航后,调用第二个主要功能:
private void SendPostRequest(string number, string message, string captcha_key, string captcha_result)
{
MessageBox.Show("Number: " + number + " Message: " + message + " key: " + captcha_key +" result: " + captcha_result);
string postData = "Body=" + message + "&Captcha=" + captcha_result + "&CheckboxTransliterate=false&Phone=" + number.Substring(3, number.Length - 3) + "&PhoneCode=" + number.Substring(0, 3) + "&WidgetId=" + captcha_key + "&_captcha_key=" + captcha_key + "&clearJson=true";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
HttpWebRequest sendRequest = WebRequest.Create("My secret url :") as HttpWebRequest;
sendRequest.Credentials = CredentialCache.DefaultCredentials;
sendRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
sendRequest.Method = "POST";
sendRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
sendRequest.Referer = "My secret url :";
sendRequest.Headers.Add("Pragma", "no-cache");
sendRequest.Headers.Add("Cache-Control", "no-cache");
sendRequest.Accept = "application/json, text/plain, *";
sendRequest.Headers.Add(HttpRequestHeader.Cookie, cookies);
sendRequest.ContentLength = bytes.Length;
sendRequest.CookieContainer = new CookieContainer();
sendRequest.CookieContainer = GetUriCookieContainer(sendRequest.RequestUri);
using (Stream dataStream = sendRequest.GetRequestStream())
{dataStream.Write(bytes, 0, bytes.Length);}
onSendCompleted(sendRequest.GetResponse().GetResponseStream());
}
private void onSendCompleted(Stream SendResponsetStream)
{
string status = new StreamReader(SendResponsetStream).ReadToEnd();
SendResponsetStream.Close();
ArgumentsClass args = new ArgumentsClass();
args.ResponseMessage = status;
args.ResponseNumber = number;
OnSmsSendend(this, args);
}
如何在其他线程中运行它的void(使用其子功能发送SMS)? 我需要这些功能(发送带有子功能的SMS)并行运行。 谢谢!
P.S。对不起我的英文:)
答案 0 :(得分:0)
由于你的SendSMS
不一定需要返回任何内容,而且参数来自集合(数组或类似的东西),你可以这样做 -
var tasks = new List<Task>();
foreach (var input in inputs)
{
tasks.Add(Task.Factory.StartNew(() => objjectInstance.SendSMS(input[info], input[browser]))); //objectInstance is class object instance
}
此处inputs
是类型SendInfo
和WebBrowser
的输入参数的集合。您必须根据需要相应地创建输入参数集合。