c#使用多线程发送HTTP请求的问题,高手进!~~~ 40
用C#做一个WinForm程序,向设置好的IP和端口发送请求,并接收响应的数据。已经实现,但是速度太慢,有什么办法能加快速度。已使用了多线程,还是慢,发送前两个很快,之后...
用C#做一个WinForm程序,向设置好的IP和端口发送请求,并接收响应的数据。已经实现,但是速度太慢,有什么办法能加快速度。
已使用了多线程,还是慢,发送前两个很快,之后就要等很长时间,要等前面的数据反回之后,才能够再发出去,这是怎么回事?怎么解决?
public void SendData()
{ for (int i = 0; i < listView1.Items.Count; i++)
{ phone = listView1.Items[i].SubItems[0].Text;
Start(phone);
}
}
public void Start(object phone)
{
....
}
不行啊,用多线程不行,改成异步发送和接收也不行,发两个就要等待,还有其它办法没有?? 展开
已使用了多线程,还是慢,发送前两个很快,之后就要等很长时间,要等前面的数据反回之后,才能够再发出去,这是怎么回事?怎么解决?
public void SendData()
{ for (int i = 0; i < listView1.Items.Count; i++)
{ phone = listView1.Items[i].SubItems[0].Text;
Start(phone);
}
}
public void Start(object phone)
{
....
}
不行啊,用多线程不行,改成异步发送和接收也不行,发两个就要等待,还有其它办法没有?? 展开
7个回答
展开全部
MSDN:
.NET Framework 类库
Thread 类
创建并控制线程,设置其优先级并获取其状态。
命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)
备注
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托或 ParameterizedThreadStart 委托指定由线程执行的程序代码。使用 ParameterizedThreadStart 委托可以将数据传递到线程过程。
在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。
GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。
注意
操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。
下面的代码示例说明简单的线程处理功能。
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: ", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
此代码产生的输出类似如下内容:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
.NET Framework 类库
Thread 类
创建并控制线程,设置其优先级并获取其状态。
命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)
备注
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托或 ParameterizedThreadStart 委托指定由线程执行的程序代码。使用 ParameterizedThreadStart 委托可以将数据传递到线程过程。
在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。
GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。
注意
操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。
下面的代码示例说明简单的线程处理功能。
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: ", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
此代码产生的输出类似如下内容:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
展开全部
能贴出点代码来吗?
在st.Write(buffer, 0, buffer.Length);后面加上st.Flush();试试。
在st.Write(buffer, 0, buffer.Length);后面加上st.Flush();试试。
追问
还是不行,发完第二个要等十几秒。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HttpWebResponse?
要是的话每次请求完了需要关闭(这个两个都关闭就好了):
httpReq.Abort(); //HttpWebRequest
httpRes.Close(); //HttpWebResponse
要是的话每次请求完了需要关闭(这个两个都关闭就好了):
httpReq.Abort(); //HttpWebRequest
httpRes.Close(); //HttpWebResponse
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这并不是多线程:
public void SendData()
{ for (int i = 0; i < listView1.Items.Count; i++)
{ phone = listView1.Items[i].SubItems[0].Text;
ThreadPool.QueueUserWorkItem(Start, phone);
}
}
public void Start(object phone)
{
byte[] buffer = Encoding.ASCII.GetBytes(GetXML(phone.ToString()));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
request.ContentLength = buffer.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream st = request.GetRequestStream();
st.Write(buffer, 0, buffer.Length);
this.Receive(request);
}
public void SendData()
{ for (int i = 0; i < listView1.Items.Count; i++)
{ phone = listView1.Items[i].SubItems[0].Text;
ThreadPool.QueueUserWorkItem(Start, phone);
}
}
public void Start(object phone)
{
byte[] buffer = Encoding.ASCII.GetBytes(GetXML(phone.ToString()));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
request.ContentLength = buffer.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream st = request.GetRequestStream();
st.Write(buffer, 0, buffer.Length);
this.Receive(request);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没用过呵呵不好意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询