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);
        }

    }

}


注:这是在VS2005环境下的frmMain窗口的代码!

控件有:System.Windows.Forms.Button()              btnStart                       程序开始

              System.Windows.Forms.TextBox()          txtThreadNumber          使用线程数

             System.Windows.Forms.ProgressBar()    progressBar1               工作进度条

             System.Windows.Forms.TextBox()          txtRecoder                    插入纪录数

            System.Windows.Forms.TextBox()           txtInfo                           显示当前使用线程信息

           System.Windows.Forms.DataGridView()   dataGridView1             最后显示插入的数据

 posted on 2007-11-29 21:01  wskfire  阅读(351)  评论(0编辑  收藏  举报