.NET 4.x Многопоточная отправка запросов - C#
Формулировка задачи:
Народ подскажите как реализовать многопоточную отправку запросов)
Вот код
все равно медленно...(((
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
string strNewValue;
string strResponse;
int zzz = 0;
public void DoTransactions()
{
string[] ID = "".Split();
richTextBox1.Invoke(new EventHandler(delegate { ID = richTextBox1.Text.Split('\n'); }));
string[] Auth_ID;
string ids = "555";
lock (this)
{
for (int i = 0; i < ID.Length; i++)
{
Auth_ID = ID[i].Split(':');
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("........");
req.Proxy = new WebProxy("127.0.0.1", 8888);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
strNewValue = "...."+Auth_ID[0]+"....."+Auth_ID[1];
req.ContentLength = strNewValue.Length;
// Write the request
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();
// Do the request to get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
}
}
}
}
}Решение задачи: «.NET 4.x Многопоточная отправка запросов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
namespace ConsoleApplicationTest {
static class Program {
static void Main( string[] args ) {
var urls = new[] {
"http://wikipedia.org",
"http://msdn.microsoft.com",
"http://www.cyberforum.ru"
};
// Состояния асинхронных операций.
var asyncResults = new List<IAsyncResult>();
foreach ( string uri in urls ) {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create( uri );
var asyncResult = req.BeginGetResponse( null, req );
Console.WriteLine( "Запрос отправлен на " + uri );
asyncResults.Add( asyncResult );
}
// Ждём завершения всех запросов.
WaitHandle.WaitAll( asyncResults.Select(result => result.AsyncWaitHandle).ToArray() );
// Получаем ответы
var responses = new List<string>();
foreach ( IAsyncResult asyncResult in asyncResults ) {
HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState;
using ( var response = request.EndGetResponse( asyncResult ) ) {
using ( var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII ) ) {
responses.Add( reader.ReadToEnd() );
}
}
asyncResult.AsyncWaitHandle.Close();
Console.WriteLine( "Ответ получен от "+ request.RequestUri );
}
foreach ( string response in responses ) {
Console.WriteLine( response );
}
Console.ReadKey();
}
}
}