Пример #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                return;
            }
            DBControllerSet.DistanceDBController disclr = new DBControllerSet.DistanceDBController();
            int sum_db     = Convert.ToInt32(Math.Sqrt(disclr.Getsum()));
            int sum_target = Convert.ToInt32(textBox1.Text);
            int toupdate   = ((sum_target * (sum_target - 1)) / 2) - ((sum_db * (sum_db - 1)) / 2);

            SetTextBox("数据库中记录: " + sum_db + "\r\n");
            SetTextBox("需要更新条数: " + toupdate + "\r\n");
            GC.Collect();
            return;
        }
Пример #2
0
 private void button2_Click(object sender, EventArgs e)
 {
     Control.CheckForIllegalCrossThreadCalls = false;
     textBox1.ReadOnly = false;
     button6.Enabled   = false;
     button1.Enabled   = false;
     button2.Enabled   = button2.Visible = false;
     button3.Enabled   = button3.Visible = true;
     DBControllerSet.DistanceDBController distanceclr = new DBControllerSet.DistanceDBController();
     richTextBox1.Text = "";
     citys             = null;
     eq            = 0;
     DistanceArray = null;
     countdown     = null;
     DataBase_city = null;
     passthrough   = true;
     posarr        = null;
     GC.Collect();
     Control.CheckForIllegalCrossThreadCalls = false;
     Temp = new Thread(() =>
     {
         try
         {
             int snum   = 0;
             int sum_db = Convert.ToInt32(Math.Sqrt(distanceclr.Getsum()));
             Control.CheckForIllegalCrossThreadCalls = false;
             SetTextBox("正在试图连接地图更新服务器......\r\n");
             ThreadPool.SetMinThreads(20, 20);
             ThreadPool.SetMaxThreads(60, 60);
             countdown  = new MutipleThreadResetEvent(1);
             object obj = countdown;
             ThreadPool.QueueUserWorkItem(new WaitCallback(Pingtest), obj);
             countdown.WaitAll();
             if (!pingtestpass)
             {
                 button2.Enabled = button2.Visible = false;
                 button3.Enabled = button3.Visible = false;
                 button1.Enabled = true;
                 return;
             }
             citys = inputline.Split(' ');
             SetTextBox("准备更新坐标......\r\n");
             SetProgressBar(3);
             DataBase_city = inputline_backup.Split(' ');
             posarr        = new pos[DataBase_city.Length];
             if (textBox1.Text == "")
             {
                 eq = citys.Length;
             }
             else
             {
                 eq = Convert.ToInt32(textBox1.Text);
             }
             countdown = new MutipleThreadResetEvent(eq);
             for (int i = 0; i < eq; i++)
             {
                 object[] objectArray = new object[2];
                 objectArray[0]       = i;
                 objectArray[1]       = countdown;
                 object param         = (object)objectArray;
                 ThreadPool.QueueUserWorkItem(new WaitCallback(Update_Position), param);
             }
             SetTextBox("开始更新坐标......\r\n");
             countdown.WaitAll();
             SetTextBox("坐标更新完毕......\r\n");
             SetProgressBar(10);
             SetTextBox("准备更新距离数据......\r\n");
             SetTextBox("共有" + eq + "个地区需要更新......\r\n");
             SetTextBox("正在提取数据库,请稍候......\r\n");
             DistanceArray = new int[eq, eq];
             DistanceArray.Initialize();
             DataTable table = new DataTable();
             distanceclr.GetTableAll(ref table);
             int toupdate = ((eq * (eq - 1)) / 2) - ((sum_db * (sum_db - 1)) / 2);
             SetTextBox("数据库提取完毕......\r\n");
             SetTextBox("正在整理数据库,请稍候......\r\n");
             int st, sp;
             while (snum < table.Rows.Count)
             {
                 st = Array.IndexOf(DataBase_city, table.Rows[snum][0]);
                 sp = Array.IndexOf(DataBase_city, table.Rows[snum][1]);
                 DistanceArray[st, sp] = DistanceArray[sp, st] = Convert.ToInt32(table.Rows[snum][2]);
                 snum++;
             }
             SetProgressBar(20);
             SetTextBox("数据库整理完毕......\r\n");
             SetTextBox("共需要从网络更新" + toupdate + "条数据......\r\n");
             if (toupdate <= 0 || eq <= sum_db)
             {
                 goto LISPO;
             }
             countdown = new MutipleThreadResetEvent(toupdate);
             for (int i = sum_db; i < eq; i++)
             {
                 DistanceArray[i, i] = 1;
                 SetProgressBar(20 + ((i * 20) / eq));
                 for (int j = 0; j < i; j++)
                 {
                     object[] objectArray = new object[3];
                     objectArray[0]       = i;
                     objectArray[1]       = j;
                     objectArray[2]       = countdown;
                     object param         = (object)objectArray;
                     ThreadPool.QueueUserWorkItem(new WaitCallback(Update_Distance), param);
                 }
             }
             SetTextBox("开始更新坐标和距离数据......\r\n");
             countdown.WaitAll();
             for (int i = 0; i < eq; i++)
             {
                 DistanceArray[i, i] = 1;
             }
             SetTextBox("坐标和距离数据更新完成\r\n");
             SetProgressBar(40);
             while (!passthrough)
             {
                 SetTextBox("开始重新校验距离数据.....\r\n");
                 List <int> x = new List <int>();
                 List <int> y = new List <int>();
                 int ssr      = 0;
                 passthrough  = true;
                 for (int i = 0; i < eq; i++)
                 {
                     for (int j = 0; j < i; j++)
                     {
                         if (DistanceArray[i, j] == 0 || DistanceArray[j, i] == 0)
                         {
                             x.Add(i); y.Add(j); ssr++;
                         }
                     }
                 }
                 SetTextBox("有" + ssr + "条数据获取失败或出错.....\r\n");
                 countdown = new MutipleThreadResetEvent(ssr);
                 for (int j = 0; j < ssr; j++)
                 {
                     object[] objectArray = new object[3];
                     objectArray[0]       = x[j];
                     objectArray[1]       = y[j];
                     objectArray[2]       = countdown;
                     object param         = (object)objectArray;
                     ThreadPool.QueueUserWorkItem(new WaitCallback(Update_Distance), param);
                 }
                 countdown.WaitAll();
             }
             citys = null;
             SetTextBox("坐标和距离数据校验成功\r\n");
             SetProgressBar(50);
             SetTextBox("构造本地数据库.....\r\n");
             DataTable newtable = table.Clone();
             newtable.Clear();
             DataRow dataRow = table.NewRow();
             for (int i = 0; i < eq; i++)
             {
                 dataRow[0] = DataBase_city[i];
                 dataRow[1] = DataBase_city[i];
                 dataRow[2] = DistanceArray[i, i];
                 lock (newtable){ newtable.Rows.Add(dataRow.ItemArray); }
                 for (int j = 0; j < i; j++)
                 {
                     dataRow[0] = DataBase_city[i];
                     dataRow[1] = DataBase_city[j];
                     dataRow[2] = DistanceArray[i, j];
                     lock (newtable) { newtable.Rows.Add(dataRow.ItemArray); }
                     dataRow[0] = DataBase_city[j];
                     dataRow[1] = DataBase_city[i];
                     dataRow[2] = DistanceArray[j, i];
                     lock (newtable) { newtable.Rows.Add(dataRow.ItemArray); }
                 }
             }
             SetTextBox("构造本地数据库完成\r\n");
             SetTextBox("开始更新本地数据库.....\r\n");
             distanceclr.Clear();
             distanceclr.WriteTable(newtable);
             SetTextBox("已写入数据库!\r\n");
             LISPO:
             SetProgressBar(100);
             SetTextBox("数据更新完成\r\n");
             button6.Enabled = button6.Visible = true;
             button3.Enabled = false;
             try
             {
                 countdown     = null;
                 DistanceArray = null;
             }
             catch (Exception err)
             {
                 if (err.Message != "正在中止线程。")
                 {
                     MessageBox.Show(err.StackTrace + "\r\n" + err.Message, "close");
                 }
             }
             button2.Enabled = button2.Visible = true;
             button3.Enabled = button3.Visible = false;
             button1.Enabled = true;
             GC.Collect();
             return;
         }
         catch (Exception err)
         {
             if (err.Message != "正在中止线程。")
             {
                 MessageBox.Show(err.StackTrace + "\r\n" + err.Message, "click");
             }
         }
     });
     Temp.IsBackground = true;
     Temp.Start();
 }