C#网络程序设计1-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.Threading;

namespace 文本抄写员程序_多线程并发执行_
{
    public partial class Form1 : Form
    {
        private delegate void WriteTextBox(char ch);
        private WriteTextBox writeTextBox;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart doTask1 = new ThreadStart(DoTsk1);
            Thread tsk1Thread = new Thread(doTask1);
            tsk1Thread.Start();
            ThreadStart doTsk2 = new ThreadStart(DoTsk2);
            Thread tsk2Thread = new Thread(doTsk2);
            tsk2Thread.Start();

        }
        private void DoTsk1()
        {
            if (checkBox1.Checked == true)
            {
                textBox1.Clear();
                textBox1.Refresh();
                writeTextBox = new WriteTextBox(WriTextBox1);
                WriTxt(writeTextBox);
                textBox3.Focus();
                textBox3.SelectAll();
            }
 
        }
        private void DoTsk2()
        {
            if (checkBox2.Checked == true)
            {
                textBox2.Clear();
                textBox2.Refresh();
                writeTextBox = new WriteTextBox(WriTextBox2);
                WriTxt(writeTextBox);
                textBox3.Focus();
                textBox3.SelectAll();
            }
        }
        private void WriTxt(WriteTextBox wMethod)
        {
            string strdata = textBox3.Text;
            for (int i = 0; i < strdata.Length; i++)
            {
                wMethod(strdata[i]);
                DateTime now = DateTime.Now;
                while (now.AddSeconds(1) > DateTime.Now)
                { }
            }

        }
        private void WriTextBox1(char ch)
        {
            textBox1.AppendText(ch + "\r");
        }
        private void WriTextBox2(char ch)
        {
            textBox2.AppendText(ch + "\r");
        }
    }
}

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