用了线程发生"未将对象的引用设置到对象的实例"
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using System.IO;using System.Data.SqlClient;namespace 多线程综合{ public partial class Form1 : Form { public Form1() { InitializeComponent(); //Control.CheckForIllegalCrossThreadCalls = false; } int index; string tb1name; string str; DataTable dt; SqlBulkCopy sbc; SqlTransaction sqltran; SqlConnectionStringBuilder sqlconnsb = new SqlConnectionStringBuilder(); SqlConnection conn; public ManualResetEvent mre = new ManualResetEvent(false); //public ManualResetEvent mre2 = new ManualResetEvent(false); private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "请选择文件"; ofd.CheckFileExists = true; ofd.Filter = "All Files|*.*"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { textBox1.Text = ofd.FileName; index = this.textBox1.Text.LastIndexOf("\\"); tb1name = textBox1.Text; } else { return; } } public void traversal() { Console.WriteLine("process in traversal"); SqlDataAdapter sda = new SqlDataAdapter(); int loop = 0; string filename; string packagequeue; string jlfsrq; DataSet ds = new DataSet("details"); DataRow dr; string srtxt; string[] Myfolder = Directory.GetFiles(tb1name.Substring(0, index), "FH*A"); using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=InsertTest;Integrated Security=True")) { for (int i = loop; i < Myfolder.Length; i++) { FileInfo fi = new FileInfo(Myfolder[i]); if (fi.FullName == tb1name) { loop = i + 1; str = "select * from details"; sda = new SqlDataAdapter(str, conn); sda.FillSchema(ds, SchemaType.Mapped, "details"); dt = ds.Tables[0].Clone();//复制表结构 ds.Dispose(); filename = this.textBox1.Text.Substring(index + 1, tb1name.Length - index - 1); jlfsrq = "20" + filename.Substring(2, 6).Trim(); packagequeue = filename.Substring(21, 3).Trim(); using (StreamReader sr = new StreamReader(textBox1.Text)) { int a = 0; while (!sr.EndOfStream) { srtxt = sr.ReadLine().Trim(); if (a > 0) { dr = dt.NewRow(); dr["hh"] = Convert.ToInt32(srtxt.Substring(11, 8)); //Console.WriteLine(Convert.ToInt32(srtxt.Substring(11, 8))); dr["stationid"] = srtxt.Substring(21, 6); //Console.WriteLine(srtxt.Substring(21, 6)); dr["sskh"] = srtxt.Substring(35, 8); dt.Rows.Add(dr); } a++; } }//sr ThreadWithState tws = new ThreadWithState(dt); mre.Set(); }//sqlconn } } } public void bulkinsert(DataTable insertdt) { mre.WaitOne(); using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=InsertTest;Integrated Security=True")) { Console.WriteLine("process in bulkinsert"); conn.Open(); sqltran = conn.BeginTransaction(); sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity, sqltran); sbc.BatchSize = 100000; sbc.BulkCopyTimeout = 50000; sbc.DestinationTableName = "details"; sbc.ColumnMappings.Clear(); sbc.ColumnMappings.Add("hh", "hh"); sbc.ColumnMappings.Add("stationid", "stationid"); sbc.ColumnMappings.Add("sskh", "sskh");//......省略相同的其他字段 try { sbc.WriteToServer(insertdt); sqltran.Commit(); } catch (Exception ex) { sqltran.Rollback(); throw new Exception(ex.Message); } finally { sbc.Close(); dt.Dispose(); //GC.GetTotalMemory(true); //GC.Collect(); //GC.WaitForPendingFinalizers(); //mre2.Set(); } }//sqlconn } private void button2_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2)); //Console.WriteLine("ThreadProc is doing"); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); } public void ThreadProc(Object stateInfo) { ThreadWithState tws = (ThreadWithState)stateInfo; Console.WriteLine("process in ThreadProc"); bulkinsert(tws.dt); } public void ThreadProc2(Object stateinfo) { Console.WriteLine("process in ThreadProc2"); traversal(); } } public class ThreadWithState { public DataTable dt; public ThreadWithState(DataTable dt) { this.dt = dt; Console.WriteLine("process in ThreadWithState"); } }}