大家好,有一个线程,在循环内执行,完成一次线程时间长短不一定,执行完成后再进入下一循环参数,如何实现,多谢!!
这是一个根据IP获取详细信息的程序,参数是IP起始和结束地址段,请问如何修改,代码如下:
private List<Thread> ThreadList;
private BindingList<taobaoIPdata> taobaoIPdataList;
private void Load(string beginIP,string endIP)
{
StartIP = IPHelper.Ip2Long(beginIP);
CurrentCollectIP = StartIP - 1;
EndIP = IPHelper.Ip2Long(endIP);
ThreadCount = (int)this.nud_ThreadCount.Value;
taobaoIPdataList = new BindingList<taobaoIPdata>();
ThreadList = new List<Thread>();
ThreadList.Add(new Thread(GetTaobaoData) { IsBackground = true });
}
private void btn_begin_Click(object sender, EventArgs e)
{
Load( txt_Start.Text , txt_End.Text);
thread.Start();
}
private long GetCurrentIp()
{
long curip = System.Threading.Interlocked.Increment(ref CurrentCollectIP);
return curip;
}
/// <summary>
/// 线程中采集的方法
/// </summary>
private void GetTaobaoData()
{
long currentipLong = GetCurrentIp();
while (currentipLong <= EndIP)
{
try
{
CaptureTaobaoIPData(currentipLong);
}
catch (Exception ex)
{
TextLog.SetString(currentipLong + ex.Message);
}
currentipLong = GetCurrentIp();
}
}
/// <summary>
/// 描述:线程中采集并得到IP
/// </summary>
private void CaptureTaobaoIPData(long currentipLong)
{
string ip = IPHelper.Long2Ip(currentipLong);
string url = string.Format(UrlFomat, ip);
string js =HttpHelper. HttpRequest(url, Encoding.UTF8);
taobaoIPdata m = Newtonsoft.Json.JsonConvert.DeserializeObject<TaobaoJsonData>(js).data;
m.ipLong = currentipLong;
taobaoIPdataList.Add(m);
//todo: 存储IP到数据库
}
- C# System.Threading.ThreadAbortEXception
- java 线程类无法注入问题,求解决
- C# 用来接收消息的Socket在结束后,接收消息的线程是否会停止并被回收
- 为什么set get 不能一直保存变量值呢
- join方法内部实际是wait方法,不能理解的是 在a线程中,b线程调用了join方法,为什么是a线
class ThreadA{
run(){
//dosth
if(){
new ThreadA().start();
}
}
}
main{
new ThreadA().start();
}
for()
{
task.factory.startnew(()=>{
//dosomething...
});
task.wait();
}
用变量控制吧。我就这样。
一个整数变量和后面的两个操作就ok.
Interlocked.Increment
Interlocked.Decrement
Task task.Wait就可以啊。 不是有人说了吗、