为什么用线程就不能给全局变量DS(DataSet)加入表呢??<pre lang="x-c#"> void loaddata() { SqlConnection cn = new SqlConnection(cnstr); if (cn.State == ConnectionState.Closed) cn.Open(); string sel = &quot;select invcode,invname,invspec,invunit from inventory&quot;; SqlCommand cmd = new SqlCommand(sel, cn); SqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(&quot;zjh&quot;); dt.Columns.Add(&quot;a&quot;, typeof(string)); dt.Columns.Add(&quot;b&quot;, typeof(string)); dt.PrimaryKey = new DataColumn[] { dt.Columns[&quot;a&quot;] }; while (dr.Read()) { DataRow r = dt.NewRow(); r[&quot;a&quot;] = dr[0].ToString(); r[&quot;b&quot;] = dr[1].ToString(); dt.Rows.Add(r); Thread.Sleep(1); } cn.Dispose(); ds.Tables.Add(dt); } void addlistviewData(ListView lv) { DataTable dx = new DataTable(); dx = ds.Tables[&quot;zjh&quot;]; //这里Ds没有表,用ds.Tables.Count.ToString(); 值为0 for (int i = 0; i &lt; dx.Rows.Count; i++) { ListViewItem lvv = new ListViewItem(dx.Rows[i][0].ToString()); lvv.SubItems.Add(dx.Rows[i][1].ToString()); lv.Items.Insert(lv.Items.Count, lvv); } } DataSet ds = new DataSet(); Thread th = null; private void Form1_Load(object sender, EventArgs e) { listView1.View = View.Details; listView1.FullRowSelect = true; listView1.GridLines = true; listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); } private void button1_Click(object sender, EventArgs e) { //为什么用线程就不能给变量DS(DataSet)加入表呢?? //直接调用loaddata()后,再用addlistviewData(listView1);却是可以的 th = new Thread(new ThreadStart(dosome)); timer1.Enabled = false; textBox2.Text = &quot;this is ok&quot;; addlistviewData(listView1); } private void dosome() { this.Invoke(new Action(loaddata)); }</pre>© 2009 Microsoft Corporation. Wszelkie prawa zastrzeżone.Sun, 19 Jul 2009 13:37:12 Z9feae571-1f27-4e83-9a8c-ecb609b6e3cdhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#9feae571-1f27-4e83-9a8c-ecb609b6e3cdhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#9feae571-1f27-4e83-9a8c-ecb609b6e3cdzjh111https://social.microsoft.com:443/profile/zjh111/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??<pre lang="x-c#"> void loaddata() { SqlConnection cn = new SqlConnection(cnstr); if (cn.State == ConnectionState.Closed) cn.Open(); string sel = &quot;select invcode,invname,invspec,invunit from inventory&quot;; SqlCommand cmd = new SqlCommand(sel, cn); SqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(&quot;zjh&quot;); dt.Columns.Add(&quot;a&quot;, typeof(string)); dt.Columns.Add(&quot;b&quot;, typeof(string)); dt.PrimaryKey = new DataColumn[] { dt.Columns[&quot;a&quot;] }; while (dr.Read()) { DataRow r = dt.NewRow(); r[&quot;a&quot;] = dr[0].ToString(); r[&quot;b&quot;] = dr[1].ToString(); dt.Rows.Add(r); Thread.Sleep(1); } cn.Dispose(); ds.Tables.Add(dt); } void addlistviewData(ListView lv) { DataTable dx = new DataTable(); dx = ds.Tables[&quot;zjh&quot;]; //这里Ds没有表,用ds.Tables.Count.ToString(); 值为0 for (int i = 0; i &lt; dx.Rows.Count; i++) { ListViewItem lvv = new ListViewItem(dx.Rows[i][0].ToString()); lvv.SubItems.Add(dx.Rows[i][1].ToString()); lv.Items.Insert(lv.Items.Count, lvv); } } DataSet ds = new DataSet(); Thread th = null; private void Form1_Load(object sender, EventArgs e) { listView1.View = View.Details; listView1.FullRowSelect = true; listView1.GridLines = true; listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); listView1.Columns.Add(&quot;aa&quot;, 100, HorizontalAlignment.Left); } private void button1_Click(object sender, EventArgs e) { //为什么用线程就不能给变量DS(DataSet)加入表呢?? //直接调用loaddata()后,再用addlistviewData(listView1);却是可以的 th = new Thread(new ThreadStart(dosome)); timer1.Enabled = false; textBox2.Text = &quot;this is ok&quot;; addlistviewData(listView1); } private void dosome() { this.Invoke(new Action(loaddata)); }</pre>Sat, 18 Jul 2009 09:08:12 Z2009-07-18T09:08:12Zhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#aef59baa-a7ca-44f8-884b-f90597e1a763https://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#aef59baa-a7ca-44f8-884b-f90597e1a763Jiyuanhttps://social.microsoft.com:443/profile/jiyuan/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??您好,<br/>当在th = <span style="color:blue">new</span> Thread(<span style="color:blue">new</span> ThreadStart(dosome));这里启用多线程时。主窗口并不阻塞。代码继续运行。<br/>所以到addlistviewData(listView1);这句时,loaddata还没来得及加载数据。所以<span style="color:#008000">Ds没有表。</span><br/>这里面有时间先后的问题。在代码中并没有保证loaddata先执行,addlistviewData后执行。<br/>如果在此基础上修改,可以用AutoResetEvent来重新设计,目的是在addlistviewData(listView1);执行前等待loaddata执行完毕。<br/><br/>个人建议修改方案:<br/>1、声明一个AutoResetEvent m_AutoResetEvent = new AutoResetEvent(false);<br/>2、在loaddata方法的最后一行增加 m_AutoResetEvent.Set(); <br/>3、在button1_Click里addlistviewData(listView1);调用前增加m_AutoResetEvent.WaitOne();例如:<br/>     <span style="color:blue">private</span> <span style="color:blue">void</span> button1_Click(<span style="color:blue">object</span> sender, EventArgs e)<br/>        {         <br/>           <br/>            th = <span style="color:blue">new</span> Thread(<span style="color:blue">new</span> ThreadStart(dosome));<br/>            timer1.Enabled = <span style="color:blue">false</span>;<br/>            textBox2.Text = <span style="color:#a31515">&quot;this is ok&quot;</span>; <br/>            ...... //在这里可以放更多其它的代码,这样设计成多线程才有意义         <br/>            m_AutoResetEvent.WaitOne(); <br/>            addlistviewData(listView1);<br/>        }<br/><br/>4、修改dosome 直接调用loaddata,不用通过Invoke来调用,只有在更新窗口控件的情况下才调用Invoke。因为您的代码loaddata并没有更新窗体控件,调用Invoke反而会使得线程重新邦回主线程。关于Invoke的机制,我在论坛中曾讨论过,请参考:<a href="http://social.microsoft.com/Forums/zh-CN/2212/thread/b55060e1-6ae7-4f54-91a8-459faf64ab2a">http://social.microsoft.com/Forums/zh-CN/2212/thread/b55060e1-6ae7-4f54-91a8-459faf64ab2a</a><br/><br/><br/>Sat, 18 Jul 2009 11:23:32 Z2009-07-18T11:23:32Zhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#00456593-787f-47de-9aa7-b8caacb20473https://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#00456593-787f-47de-9aa7-b8caacb20473zjh111https://social.microsoft.com:443/profile/zjh111/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??有没有好的办法, 我在后台加载数据到DataSEt的表中,但界面的其它控件不卡,也可以操作,窗体也可以移动,如在另一个TextBox中录数据等..<br/> 谢谢!!Sat, 18 Jul 2009 16:01:02 Z2009-07-18T16:01:02Zhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#7a667241-6ae6-4a24-a4de-01c56d607555https://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#7a667241-6ae6-4a24-a4de-01c56d607555Jiyuanhttps://social.microsoft.com:443/profile/jiyuan/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??最简单的做法:不用我在上面的建议做法而是在您原来的代码上把addlistviewData(listView1);移动到loaddata方法的最后一行。Sat, 18 Jul 2009 16:44:13 Z2009-07-18T17:01:54Zhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#dd8ad278-eb3a-4347-8726-1cd0ff2959f7https://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#dd8ad278-eb3a-4347-8726-1cd0ff2959f7zjh111https://social.microsoft.com:443/profile/zjh111/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??<blockquote>最简单的做法:不用我在上面的建议做法而是在您原来的代码上把addlistviewData(listView1);移动到loaddata方法的最后一行。</blockquote> 我说的是加载Ds而不是显示在ListView上<br/>Sun, 19 Jul 2009 05:08:19 Z2009-07-19T05:08:19Zhttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#63c3734d-ff17-43de-a8a2-c21fba3735behttps://social.microsoft.com/Forums/pl-PL/9feae571-1f27-4e83-9a8c-ecb609b6e3cd/dsdataset?forum=2212#63c3734d-ff17-43de-a8a2-c21fba3735beJiyuanhttps://social.microsoft.com:443/profile/jiyuan/?type=forum为什么用线程就不能给全局变量DS(DataSet)加入表呢??<blockquote> <blockquote>最简单的做法:不用我在上面的建议做法而是在您原来的代码上把addlistviewData(listView1);移动到loaddata方法的最后一行。</blockquote> 我说的是加载Ds而不是显示在ListView上<br/></blockquote> <br/>如果单说加载,您自己发布的代码就可以,现在的问题不就是因为要显示的时候数据还没加载造成的吗?<br/>把加载和显示放在同一个线程上才能保证执行的先后顺序。Sun, 19 Jul 2009 13:37:12 Z2009-07-19T13:37:12Z Wewnętrzny błąd serwera