首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

用了线程产生"未将对象的引用设置到对象的实例"

2012-07-15 
用了线程发生未将对象的引用设置到对象的实例C# codeusing Systemusing System.Collections.Genericus

用了线程发生"未将对象的引用设置到对象的实例"

C# code
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");        }    }} 



用了2个线程是干两个动作,第一个动作是遍历文件,传递参数datatable 给第二个动作插入数据库.
第一个动作进入线程后任意一步都会出现"未将对象的引用设置到对象的实例".看了很多帖子情况都不太一样.高手帮忙看看,用力拍也没关系,多线程操作不熟悉.

[解决办法]
哪里有变量在使用前没有初始化
[解决办法]
探讨
引用:
哪里有变量在使用前没有初始化

这个是普遍回答,但是我感觉我的问题不在这里

热点排行