求助:循环执行一个线程

2019-01-02 22:04发布

大家好,有一个线程,在循环内执行,完成一次线程时间长短不一定,执行完成后再进入下一循环参数,如何实现,多谢!!

这是一个根据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到数据库
}

标签: 线程
6条回答
【Aperson】
2楼-- · 2019-01-02 22:09
public static async void Run()
        {
            var rm = new Random();
            for (var i = 0; i < 100; i++)
            {
                await Task.Run(async () =>
                {
                    await Task.Delay(rm.Next(200, 1000));
                    Console.WriteLine(i);
                });
            }
        }
查看更多
爷的心禁止访问
3楼-- · 2019-01-02 22:11

class ThreadA{

    run(){

   //dosth

   if(){

    new ThreadA().start();

     }

   }

}

main{

    new ThreadA().start();

}

查看更多
男人必须洒脱
4楼-- · 2019-01-02 22:14

for()

{

task.factory.startnew(()=>{

//dosomething...

});

task.wait();

}

查看更多
We Are One
5楼-- · 2019-01-02 22:16

用变量控制吧。我就这样。

一个整数变量和后面的两个操作就ok.

Interlocked.Increment

Interlocked.Decrement

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-02 22:28

Task  task.Wait就可以啊。 不是有人说了吗、

查看更多
做个烂人
7楼-- · 2019-01-02 22:32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            CancellationTokenSource s = new CancellationTokenSource();
            CancellationToken token = s.Token;

            for (int i = 0; i < 5000; i++)
            {
                Thread th = new Thread(new ParameterizedThreadStart((o) =>
                    {
                        Console.WriteLine(o.ToString());
                    }));
                th.Start(i);
                token.WaitHandle.WaitOne(50);
            }
        }
    }
}
查看更多
登录 后发表回答