zoukankan      html  css  js  c++  java
  • 线程的创建及其跨线程访问

              本人是搞web开发的,对于线程方面研究很少,只是知道基本的概念。在web中,异步都是用ajax来实现。最近做了个小项目是用来拆分word文档的。项目完成后,要处理几十万条的数据。这个时候再用web来实现就很不友好了。说以就想到了用winform来实现。下面来列出需求:

    1.  有个输入框,输入sql语句。
    2.  有个主键的起始,截止输入框。
    3.  开始处理的按钮。中止的按钮等。显示处理的进度条(由于处理数据较大,进度条半天也没走到,这个时候为了直观就需要使用lable来计数处理的条数)

              为了不影响窗体的运行。开始的处理需要在新的线程中运行,在处理的过程中,每处理一条数据需要对lable标签赋值(跨线程访问控件)

    以下是程序代码:

              

    namespace DateManager
    {
        public partial class Form1 : Form
        {
    
            Thread td = null;
    
            public Form1()
            {
                InitializeComponent();
                td = new Thread(begionSplit);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                td.IsBackground = true;
                td.Start();
    
            }
    
    
            public void begionSplit()
            {
                string sql = this.tb_Sql.Text;
                int begionindex = Convert.ToInt32(this.tb_pagesiez.Text == "" ? "10" : this.tb_pagesiez.Text);
                int endindex = Convert.ToInt32(this.tb_pageIndex.Text == "" ? "1" : this.tb_pageIndex.Text);
                sql = sql + " and CATInID >=" + begionindex + " and CATInID <=" + endindex + "";
                SplitLowSystem.BLL.SourceLawBLL bll = new SplitLowSystem.BLL.SourceLawBLL();
                int pagecout = 0;
                DataTable dt = SplitLowSystem.Common.DbHelperSQL.Query(sql).Tables[0];
                if (progressBar1.InvokeRequired)
                {
                    this.progressBar1.Invoke(new MethodInvoker(() => { progressBar1.Maximum = dt.Rows.Count; }));
                }
                else
                {
                    progressBar1.Maximum = dt.Rows.Count;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    try
                    {
                        if (progressBar1.InvokeRequired)
                        {
                            this.progressBar1.BeginInvoke(new MethodInvoker(() => { progressBar1.Value = i + 1; }));
                        }
                        else
                        {
                             progressBar1.Value = i + 1; 
                        }
                        if (lb_int.InvokeRequired)
                        {
                            this.lb_int.BeginInvoke(new MethodInvoker(() => { this.lb_int.Text = (i + 1).ToString(); }));
                        }
                        else
                        {
                            this.lb_int.Text = (i + 1).ToString();
                        }
                        
                        string CATInID = dt.Rows[i]["CATInID"].ToString();
                        string sqlstr = "select * from LawProv where CATInID=" + CATInID + " order by ProvNumber ";
                        DataSet dss = SplitLowSystem.Common.DbHelperSQL.Query(sqlstr);
                        bll.SplitByds(dss, CATInID);
                    }
                    catch
                    {
                        continue;
                    }
                }
                MessageBox.Show("处理完毕!");
                td.Abort();
    
            }
    
        }
    }
    

      

  • 相关阅读:
    第三章 熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习,词频统计
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
  • 原文地址:https://www.cnblogs.com/duchyaiai/p/5740456.html
Copyright © 2011-2022 走看看