C#网络程序设计1-3:线程支持

图形界面设计如图所示:
C#网络程序设计1-3:线程支持_第1张图片
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;

namespace 文本抄写员程序_线程支持_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart doTask = new ThreadStart(DoTsk);
            Thread tskThread = new Thread(doTask);
            tskThread.Start();
        }
        private void DoTsk()
        {
            if (checkBox1.Checked == true)
            {
                textBox1.Clear();
                textBox1.Refresh();
                this.writeTextBox1();
                textBox3.Focus();
                textBox3.SelectAll();
            }
            if (checkBox2.Checked == true)
            {
                textBox2.Clear();
                textBox2.Refresh();
                this.writeTextBox2();
                textBox3.Focus();
                textBox3.SelectAll();
            }
        }
        private void writeTextBox1()
        {
            string strdata = textBox3.Text;
            for (int i = 0; i < strdata.Length; i++)
            {
                textBox1.AppendText(strdata[i] + "\r"); 
                DateTime now = DateTime.Now;
                while (now.AddSeconds(1) > DateTime.Now)
                { }
            }
 
        }
        private void writeTextBox2()
        {
            string strdata = textBox3.Text;
            for (int i = 0; i < strdata.Length; i++)
            {
                textBox2.AppendText(strdata[i] + "\r");
                DateTime now = DateTime.Now;
                while (now.AddSeconds(1) > DateTime.Now)
                { }
            }
 
        }
    }
}

你可能感兴趣的:(C#网络程序设计)