我照书上写了一个启动线程的控制台例子,在VS2005里却通不过,是什么原因啊?是不是VS版本的原因,错误提示就是标题所示:
1.建一控制台应用程序,项目名为“startThread"
2.拖拽一ListBox控件放到表单中,把Form1的text属性设为“启动线程”
3.在名空间添加类Sample,代码如下:
public class Sample
    {
        private System.Windows.Forms.ListBox listBox1;        public Sample(System.Windows.Forms.ListBox a)
        {
            this.listBox1 = a;
        }
        public void work()
        {
            listBox1.Items.Add("Sample的work线程已运行0");
        }         
    }
4.Form1的Load事件添加如下代码:
public  void Form1_Load(object sender, System.EventArgs e)
        {
            Console.ReadLine();
            Sample st = new Sample(listBox1);
            Thread t = new Thread(new ThreadStart(st.work));
            t.Start();                }
5.运行,我运行的时候出现dos框一闪而过,没有看到“Sample的work线程已运行0”字样

解决方案 »

  1.   

    dos框?
    你这是控制台程序吧,是不是连form1窗体都没起来?
      

  2.   

    用控制台程序还能用上listbox这样的winform控件吗?
    连UI都没有吧,求解答
      

  3.   

    我也很奇怪呀,参见visual C#时尚编程百例第77实例
      

  4.   

    我给你个例子
    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.Text;
    using System.IO;
    using System.Threading;
    namespace Text
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            FileStream fs;
            FileStream wfs;
            byte[] filedata;
            int len;
            private void button1_Click(object sender, EventArgs e)
            {
                Thread thd, thd2;
                fs = new FileStream(@"E:\test.txt", FileMode.Open);
                wfs = new FileStream(@"E:\write.txt", FileMode.Create);
                len = (int)fs.Length;
                filedata = new Byte[len];
                fs.Read(filedata, 0, len);
                System.Threading.AutoResetEvent controlThda =
                    new System.Threading.AutoResetEvent(false);
                System.Threading.AutoResetEvent controlThdb =
                    new System.Threading.AutoResetEvent(false);
                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.MultiWritetwo), controlThda);
                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.MultiWriteone), controlThdb);
                controlThda.WaitOne();
                controlThdb.WaitOne();            wfs.Close();
                fs.Close();
                Console.WriteLine("f");        }        private void MultiWriteone(object state)
            {
                wfs.Seek(0, SeekOrigin.Begin);
                wfs.Write(filedata, 0, 5);
                Console.WriteLine("1 End");
                ((System.Threading.AutoResetEvent)state).Set();
            }
            private void MultiWritetwo(object state)
            {
                wfs.Seek(5, SeekOrigin.Begin);
                wfs.Write(filedata, 5, len - 5);
                Console.WriteLine("2 End");
                ((System.Threading.AutoResetEvent)state).Set();
            }    }
    }
      

  5.   

    如果用了UI, 就建立Winform 项目。如果用控制台项目,则用Console.Readline()来暂停程序,以便看到结果。
      

  6.   

    好象是以前的版本的问题吧,VS2003以后的VS2005可能对线程操作控件增加了安全性吧