红炉点雪

CSharp后台线程修改UI控件示例

去年写的一个批量查询IP地址归属地的例子,通过创建委托的方式修改UI控件不会造成界面卡死。学别的语言之前做一下存档。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using SufeiUtil;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace IP地址批量查询
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

string Regexip = @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";
string Regex_input = @"(?<=value="").+(?="" id=""InputIPBox)";
string Regex_span = @"(?<=<span id=""InputIPAddrMessage"">).+(?=</span>)";

//创建一个委托用来访问textBox控件。
public delegate void UpdateTxt(string msg);
//定义一个委托变量
public UpdateTxt updateTxt;

//修改TextBox值的方法。
public void UpdateTxtMethod(string res)
{
textBox2.AppendText(res);
textBox2.ScrollToCaret();
}

//此为在非创建线程中的调用方法,其实是使用TextBox的Invoke方法。
public void ThreadMethodTxt(string[] str)
{
string ip = "";

for (int i = 0; i < str.Length; i++)
{
ip = str[i].Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
var str_is_ip = Regex.IsMatch(ip, Regexip);
if (str_is_ip)
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://www.cz88.net/ip/?ip=",
Method = "post",
IsToLower = false,
Cookie = "BDPCIEXP=90; Hm_lvt_cc658daf205377fb870d72f7c9f2b59f=1541732078,1542078670,1542271117,1542874803; Hm_lpvt_cc658daf205377fb870d72f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uuuWdmzwvYj48L2E%2BPGJyIC8%2BPGEgaHJlZj0naHR0cDovL2Jicy5jejg4Lm5ldCcgdGl0bGU9J%2Biuv%2BmXrue6r%2Becn%2BaXtuepuuiuuuWdmyc%2B5Zyo4oCcSVDmnb%2FlnZfigJ3lj5HotLTvvIzosKLosKLvvIE8L2E%2BZGTHEdL5WpdSIZjBR3ptmxyhPly8NUp6YdJz0MKVmsYs0w%3D%3D&__VIEWSTATEGENERATOR=E7E90B87&__EVENTVALIDATION=%2FwEdAAY4gv4Y1gDBYGhV%2BjiKkbV6F436fNojvFeOm9eL%2FKluTdkYYat1P6GKnCUyTxAy1qUJ0cIRPc1UdcIwo%2B8k5kqrW9iHyHlfiWb1jP86NUAWgkCUG34X7JCHaEh29heWFgpSuPYOkN5fJ83toKq0ZnjQfISxJCrECZbQ6zlErjo9fA%3D%3D&InputIPBox=" + ip + "&InputIPButton=%B2%E9+%D1%AF&ReportIPBox=&ReportAddrBox=",//Post数据
Timeout = 10000,//连接超时时间
ReadWriteTimeout = 10000,//写入Post数据超时时间
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
ContentType = "application/x-www-form-urlencoded",
Allowautoredirect = false,
ProxyIp = "",
ResultType = ResultType.String
};
item.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
item.Header.Add("Accept-Encoding", "gzip, deflate");
item.Header.Add("DNT", "1");
item.Header.Add("Upgrade-Insecure-Requests", "1");

HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;

string IP = Regex.Match(html, Regex_input).ToString();
string IPAddr = Regex.Match(html, Regex_span).ToString();
string IPStr = IP + "\t" + IPAddr + "\r\n";

this.BeginInvoke(updateTxt, IPStr);
}
else
{
if (ip != "")
{
this.BeginInvoke(updateTxt, ip + "\t【查询失败。】\r\n");
}
}
}
}

private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split('\n');

Thread objThread = new Thread(new ThreadStart(delegate
{
ThreadMethodTxt(str);
}));
objThread.Start();
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
{
((TextBox)sender).SelectAll();
}
}

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
{
((TextBox)sender).SelectAll();
}
}

private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}

private void button3_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(textBox2.Text);
}

private void MainForm_Load(object sender, EventArgs e)
{
//实例化委托
updateTxt = new UpdateTxt(UpdateTxtMethod);
}
}
}
CONTACT