using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
delegate void AppendStringDelegate(string str);
AppendStringDelegate appendStringDelegate;
public Form1()
{
InitializeComponent();
appendStringDelegate = new AppendStringDelegate(AppendString);
}
public StringBuilder sb = new StringBuilder();
Thread thread1;
Thread thread2;
private void AppendString(string s)
{
richTextBox1.Text += s;
}
public void Method1()
{
while (true)
{
Thread.Sleep(100);
richTextBox1.Invoke(appendStringDelegate, "a");
}
}
public void Method2()
{
while (true)
{
Thread.Sleep(100);
richTextBox1.Invoke(appendStringDelegate, "b");
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
thread1 = new Thread(new ThreadStart(Method1));
thread2 = new Thread(new ThreadStart(Method2));
thread1.Start();
thread2.Start();
}
private void button2_Click(object sender, EventArgs e)
{
thread1.Abort();
thread1.Join(10);
thread2.Abort();
thread2.Join(10);
MessageBox.Show("线程1、2终止成功");
}
}
}