MainForm.
one textbox,one button.Following is the code:    public partial class Form1 : Form
    {
        public delegate void WriteString(string str);
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
           
            Thread thread1 = new Thread(new ThreadStart(SafeMethod));
            
            thread1.Start();
        }        private void SafeMethod()
        {
           
            this.WriteStringToTextBox("s");
        }        private void WriteStringToTextBox(string str)
        {
          
            if (this.textBox1.InvokeRequired)
            {
                WriteString ws = new WriteString(WriteStringToTextBox);
                this.Invoke(ws, new object[] { str });
            }
            else
            {
                textBox1.AppendText(str);
            }
        }
    }
 But when I click the button,it shows only one "s" in the textbox.
 Why it doesnot go on appending "s" to the textbox???

解决方案 »

  1.   

    Maybe I can add a loop:
      private void SafeMethod()
            {
                string[] strArray=new string[20];
                for (int i = 0; i < strArray.Length; i++)
                {
                    Thread.Sleep(1000);
                    this.WriteStringToTextBox("s");
                }
            }There is another button named pauseButton.When I press it,it paused to append text to the textbox.
    So I donot want to use a fixed string Array.Any other way?
      

  2.   

    你是不是哪里其它地方有问题,你这个代码我运行了,没问题
    能连续输入sssss...
      

  3.   

    To stg609:
    I see.but if i donot use the fixed-length array,it couldnot.