C# Web 浏览器和多线程



我有一个类,其主要功能是:

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(发送带有其子函数的短信)?我需要并行运行这些功能(发送短信及其子函数)。谢谢!

附言对不起我的英语:)

由于您的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是类型SendInfoWebBrowser输入参数的集合。您必须根据需要相应地创建输入参数集合。

相关内容

  • 没有找到相关文章

最新更新