IP代理管理器編寫

上一篇寫的是如何驗證IP代理是否用,這篇就寫個管理器吧。

源碼:


    public partial class AgentManage : Form
    {
        public AgentManage()
        {
            InitializeComponent();
        }
        DataTable ipTable = new DataTable();
        private void AgentManage_Load(object sender, EventArgs e)
        {
            ipTable.Columns.Add("地址", typeof(string));
            ipTable.Columns.Add("端口", typeof(string));
            ipTable.Columns.Add("狀態", typeof(string));
            ipTable.Columns.Add("是否可用", typeof(bool));
            ipTable.Columns.Add("結果", typeof(string));
            dataGridView1.DataSource = ipTable;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AutoGenerateColumns = false;
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            string ipListStr = this.richTextBox_ipList.Text.Trim();
            if (ipListStr == "")
            {
                return;
            }
            else {
                String guize = this.comboBox1.SelectedItem.ToString();
                if (guize.Contains("\\n")) {
                    guize = "\n";
                }
                if (guize.Contains(","))
                {
                    guize = ",";
                }
                String [] arr=ipListStr.Split(guize.ToCharArray());
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] == "") {
                        continue;
                    }
                    if (!arr[i].Contains(":")) {
                        continue;
                    }
                    String[] arrIP = arr[i].Split(':');
                    try
                    {
                        this.Invoke(new Action(() =>
                        {
                            ipTable.Rows.Add(arrIP[0], arrIP[1], "等待驗證", false);
                        }));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        //驗證所有
        private void button_verifyAll_Click(object sender, EventArgs e)
        {
            Thread filter = new Thread(new ThreadStart(filterIP)) { IsBackground = true };
            filter.Start();
          
        }

        private void filterIP()
        {
            List<System.Threading.Tasks.Task> TaskList = new List<System.Threading.Tasks.Task>();

                for (int i = 0; i < this.dataGridView1.Rows.Count-1; i++)
                {
                    Console.Write(i.ToString());
                    if (i == 0)
                        continue;

                this.dataGridView1.Rows[i].Cells[2].Value = "驗證中。。。";

     
                    String ip = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                    String port = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
                    if (ip == "" || port == "")
                        continue;

                    var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        IPResult iPResult = new IPResult();
                        iPResult.Ip = ip;                  
                        iPResult.Index = i;
                        var result = CommonTools.WebRequestAction.VerIP(ip, int.Parse(port));
                        iPResult.Result = result;                    
                        return iPResult;

                    }).ContinueWith(obj => {
                        if (obj.Status == TaskStatus.RanToCompletion)
                        {
                            this.Invoke(new Action(() =>
                            {
                                 this.dataGridView1.Rows[obj.Result.Index].Cells[2].Value = "已驗證";
                                this.dataGridView1.Rows[obj.Result.Index].Cells[3].Value = obj.Result.Result;
                                this.dataGridView1.Rows[obj.Result.Index].Cells[4].Value = obj.Result.Result.ToString();
                                Console.WriteLine("ip:"+ ip+"結果" + obj.Result.Result+"i:"+ obj.Result.Index.ToString());
                            }));                      
                        }
                        else {
                            this.dataGridView1.Rows[i].Cells[4].Value = obj.Status.ToString();
                        }
                    });
                    TaskList.Add(task);
                }
          
            System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());
        }

      
    }
    public class IPResult {
        private String ip;
        private Boolean result;
        private int index;

        public string Ip { get => ip; set => ip = value; }
        public bool Result { get => result; set => result = value; }
        public int Index { get => index; set => index = value; }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章