using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace TreadEx
{
class Class1
{
public volatile bool shouldstop;
private Form1 form1;
public Class1(Form1 form1)
{
this.form1 = form1;
}
public void Method1(object obj)
{
string s = obj as string;
form1.AddMessage(s);
while (shouldstop==false)
{
Thread.Sleep(100);
form1.AddMessage("a");
}
form1.AddMessage("\n 线程Method1已终止");
}
public void Method2()
{
while (shouldstop == false)
{
Thread.Sleep(100);
form1.AddMessage("b");
}
form1.AddMessage("\n 线程Method2已终止");
}
}
}
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 TreadEx
{
public partial class Form1 : Form
{
Thread thread1, thread2;
Class1 class1;
public Form1()
{
InitializeComponent();
class1 = new Class1(this);
button1.Click+=new EventHandler(button1_Click);//委托button1_Click订阅事件button1.Click,即执行1次