private static void SendCallBack(IAsyncResult result) { AsyncResult resultObj = (AsyncResult)result; SendHandler sender = (SendHandler)resultObj.AsyncDelegate;//获取原来的委托 sender.EndInvoke(resultObj); }
private void EndSend(IAsyncResult asyncResult) { if (asyncResult == null) { return; } SendHandler handler = (SendHandler)(asyncResult.AsyncState); handler.EndInvoke(asyncResult); }
private void Send_Click(object sender, EventArgs e) { if (urlTxt.Text.Length != 0) { try { //string s = HTTPHelper.SendWebHttpRequest(urlTxt.Text.Trim(), comboBox1.Text, jasonInput.Text); //jasonOutput.Text = s; //for (int i = 0; i < 100; i++) //{ // HTTPHelper.SendWebHttpRequestAsync(urlTxt.Text.Trim(), comboBox1.Text, jasonInput.Text, (wr) => // { // //MessageBox.Show("now threadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); // Console.WriteLine("now threadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); // //System.Threading.Thread.Sleep(100); // if (wr != null) // { // if (wr != null) // { // using (var httpResp = (HttpWebResponse)wr.GetResponse()) // { // if (httpResp.StatusCode == HttpStatusCode.BadRequest) // { // var buff = new byte[httpResp.ContentLength]; // using (var response = httpResp.GetResponseStream()) // { // if (response != null) // { // response.Read(buff, 0, (int)httpResp.ContentLength); // } // } // throw new InvalidOperationException( // string.Format("Some error occurred while sending request to Elasticsearch.{0}{1}", // Environment.NewLine, Encoding.UTF8.GetString(buff))); // } // else // { // using (var sr = new StreamReader(httpResp.GetResponseStream(), Encoding.UTF8)) // { // string s = sr.ReadToEnd(); // //Console.WriteLine(s); // jasonOutput.Text = s; // } // } // } // } // } // }); //} for (int i = 0; i < 10000; i++) { HTTPHelper.SendWebHttpRequestFullAsync(urlTxt.Text.Trim(), comboBox1.Text, jasonInput.Text, delegate(IAsyncResult ar) { Console.WriteLine("CallBack threadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); if (ar == null) { return; } CallBacks.WebRequestCallBacks(ar, new MyProcess()); AsyncResult arr = ar as AsyncResult; SendHandler handler = arr.AsyncDelegate as SendHandler; handler.EndInvoke(ar); }); Console.WriteLine("III threadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); } } catch (Exception ex) { jasonOutput.Text = ex.ToString(); } finally { } } }