窗口包含一个文本框用于显示网络请求数据,一个开始按钮,一个停止按钮;
“开始”后,程序按照设定值顺序开启线程,每个线程执行网络数据请求,请求完后将获取到的数据连同进程名称传递给主进程显示到文本框。可以根据需要设定请求url和线程数。
代码如下:
using System;
using System.Threading;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class ThreadTEST : Form
{
public ThreadTEST()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//开始按钮
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
Thread TT = new Thread(new ThreadStart(threadStart));
TT.Name = "ThreadStart";
TT.Start();
}
//停止按钮
private void button2_Click(object sender, EventArgs e)
{
doSomethingNetWorkClass.stop = true;
button2.Enabled = false;
button1.Enabled = true;
}
//子线程委托回调主线程数据处理
public delegate void SetThreadData(string threadName, string threadData);
public void SetThreadDataMethed(string threadName,string threadData)
{
//listView1.Items.Add(threadName);
if (this.textBox1.InvokeRequired)
{
SetThreadData addthreadtext = new SetThreadData(SetThreadDataMethed);
this.Invoke(addthreadtext, threadName, threadData);
}
else
{
textBox1.AppendText(threadName+"::::::"+threadData + Environment.NewLine);
//....do something else
}
}
private void threadStart()
{
//设定进程数,根据需要取值
int cnt = 10;
int i = 0;
while (i < cnt)
{
doSomethingNetWorkClass dsc = new doSomethingNetWorkClass();
dsc.parentF = this;
Thread TT = new Thread(new ThreadStart(dsc.doSomethingNetWork));
TT.Name = "Thread" + i;
TT.Start();
i++;
System.Threading.Thread.Sleep(1000);
}
}
private void ThreadTEST_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
}
public class doSomethingNetWorkClass
{
public ThreadTEST parentF;
public static bool stop = false;
public static bool pause = false;
public doSomethingNetWorkClass()
{
}
public void doSomethingNetWork()
{
Thread t = Thread.CurrentThread;
//thread dosomething
//thread net request
string url = "http://5.push2.eastmoney.com/api/qt/list.np/get?fid=f3&z=40&po=1&ut=bd1d9ddb0427f6f7426281&fltt=2&filds=parentF4,parentF2,parentF3,f2,f3,f4,f6,parentF04,parentF05,parentF0&secids=100.TOP40%2C100.AS51%2C100.ATX%2C100.BFX%2C100.BVSP%2C100.TSX%2C100.PX%2C100.FCHI%2C100.HEX%2C100.GDAXI%2C100.AEX%2C100.ASE%2C100.SENSEX%2C100.ICEXI%2C100.JKSE%2C100.N225%2C100.KS11%2C100.ISEQ%2C100.MIB%2C100.KLSE%2C1100.SSMI%2C100.SET%2C100.TWII%2C100.FTSE%2C100.DJIA%2C100.NDX%2C100.SPX%2C100.VNINDEX&_=1653985514051";
string resultStr = "";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Timeout = 5000;
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
//请求一次数据
//System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
//System.IO.Stream myResponseStream = response.GetResponseStream();
//System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream, Encoding.UTF8);
//resultStr = myStreamReader.ReadToEnd();
//一直请求数据
while (1 == 1)
{
if (pause)
{
Thread.Sleep(5000);
continue;
}
if (stop)
{
break;
}
Console.WriteLine("Thread working..." + t.Name);
try
{
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream myResponseStream = response.GetResponseStream();
System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream, Encoding.UTF8);
resultStr = myStreamReader.ReadToEnd();
if (resultStr == null)
continue;
parentF.SetThreadDataMethed(t.Name, resultStr);
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//throw new Exception(ex.Message);
}
}//while
}//doSomethingNetWork
}//class
}