zoukankan      html  css  js  c++  java
  • Thread 备忘录

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Threading;

    namespace AppThread
    {
        
    public delegate void SetProcessBar();
        
    public delegate void SetText(string Text);

        
    public partial class frmMain : Form
        
    {
            
    public frmMain()
            
    {
                InitializeComponent();
            }


            
    private void btnStart_Click(object sender, EventArgs e)
            
    {
                count 
    = 0;
                ThreadConpute();
            }

            
    private void ThreadConpute()
            
    {
                
    int threadNumber = Convert.ToInt32(txtThreadNumber.Text);
                
    for (int i = 1; i <= threadNumber; i++)
                
    {
                    ThreadStart ts 
    = new ThreadStart(Compute);
                    Thread thread 
    = new Thread(ts);
                    thread.Name 
    = i.ToString();
                    thread.Start();
                }

                Console.ReadLine();
            }

            
    private void Compute()
            
    {
                
    int threadNumber = Convert.ToInt32(txtThreadNumber.Text);
                
    int threadOrder =Convert.ToInt32( Thread.CurrentThread.Name);
                
    long Recoder = Convert.ToInt64(txtRecoder.Text);
                
    long step = Recoder / threadNumber;

                
    long startValue =1 + step * (threadOrder-1);
                
    long endValue = 0;
                
    if (threadOrder == threadNumber)
                
    {
                    endValue 
    = Recoder + 1;
                }

                
    else 
                
    {
                    endValue 
    = step + startValue;
                }

                
    for (long val = startValue; val < endValue; val++)
                
    {
                    
    lock (this)
                    
    {
                        
    string customerCode = GetCustomerCode(val);
                        
    int intResult=InsertCustomer(customerCode);
                        
    object[] obj=new object[] { Thread.CurrentThread.Name, customerCode, intResult};
                        
    string curInfo = string.Format("当前线程:{0},字段为:{1},插入成功:{2}", obj);
                        SetTextInfo(curInfo);
                        SetProcess();
                        Thread.Sleep(
    1);
                    }

                }
             
            }

            
    private void SetProcess()
            
    {
                
    if (progressBar1.InvokeRequired)
                
    {
                    
    this.Invoke(new SetProcessBar(SetProcess), null);
                }

                
    else
                
    {
                    count
    ++;
                    
    long maxValue = Convert.ToInt64(txtRecoder.Text);
                    progressBar1.Maximum 
    = Convert.ToInt32(maxValue/10);
                    progressBar1.Minimum 
    = 0;
                    progressBar1.Value 
    = Convert.ToInt32(count / 10);
                    
    if (progressBar1.Value == progressBar1.Maximum)
                    
    {
                        
    bool v = SelectValue();
                        MessageBox.Show(
    "插入成功,值有没有重复"+v);
                        progressBar1.Value 
    = 0;                    
                    }

                }
                
            }

            
    private bool SelectValue()
            
    {
                
    string sQuery;
                sQuery 
    = string.Format("select * from {0} where {1} in(select {1} from {0} group by {1} having count({1})>=2)""customer""customerCode");
                
    if (sqlCon == null)
                
    {
                    
    string strConn = "server=.;database=thread;uid=sa;pwd=;";
                    sqlCon 
    = new SqlConnection(strConn);
                }

                
    try
                
    {
                    sqlCon.Open();
                    SqlCommand sqlCmd 
    = new SqlCommand(sQuery, sqlCon);
                    Object obj 
    = sqlCmd.ExecuteScalar();
                    
    if (obj == null)
                    
    {
                        
    return false;
                    }

                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(ex.Message);
                }

                
    finally
                
    {
                    sqlCon.Close();
                }

                
    return true;
            }


            
    long count = 0;
            
    private void SetTextInfo(string text)
            
    {
                
    if (txtInfo.InvokeRequired)
                
    {
                    
    this.Invoke(new SetText(SetTextInfo), new object[] { text });
                }

                
    else
                
    {                
                    txtInfo.Text 
    = text;
                }
               
            }

            
    private SqlConnection sqlCon;
            
    private int InsertCustomer(string code)
            
    {
                
    string sQuery = "Insert Into customer(customerCode)values('@code')";
                sQuery 
    = sQuery.Replace("@code", code);
                
    if (sqlCon == null)
                
    {
                    
    string strConn = "server=.;database=thread;uid=sa;pwd=;";
                    sqlCon 
    = new SqlConnection(strConn);
                }

                SqlCommand sqlCmd 
    = new SqlCommand(sQuery, sqlCon);
                
    int intResult = 0;
                
    try
                
    {
                    sqlCon.Open();
                    intResult 
    = sqlCmd.ExecuteNonQuery();
                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(ex.Message);
                }

                
    finally 
                
    {
                    sqlCon.Close();
                }

                
    return intResult;
            }

            
    private string GetCustomerCode(long code)
            
    {
                
    string strTemp = "000000";
                strTemp 
    += code.ToString();
                
    return strTemp.Substring(strTemp.Length-7);
            }

        }

    }

  • 相关阅读:
    [CentOS] SSH 免密钥登录
    [WPF] Wait for a moment.
    [Tool] Open Live Writer 插件更新
    [Tool] 博客园的 SyntaxHighlighter 代码高亮
    [Tool] Open Live Writer插件开发
    [Oracle] Bulk Insert Data
    [C1] C1ComboBox 的非编辑状态优化
    CentOS7用阿里云Docker Yum源在线安装Docker 17.03.2
    什么时候该用readfile() , fread(), file_get_contents(), fgets()?
    Aliyun OSS Nginx proxy module(阿里云OSS Nginx 签名代理模块)
  • 原文地址:https://www.cnblogs.com/meilibao/p/2717068.html
Copyright © 2011-2022 走看看