使用多线程时出错,求指教!
程序代码:
namespace Thread { public partial class Thread : Form { public Thread() { InitializeComponent(); } //=================================以下部分为两个线程(Add,,Div)所要做的事====================================== public static void Add() { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; Console.WriteLine("Addtion:i=" + i + " sun=" + sum); } } public static void Div() { int sum = 5050; for (int i = 1; i <= 100; i++) { sum -= i; Console.WriteLine("Divistion:i=" + i + " sum=" + sum); } } //=================================以上部分为两个线程的运算=============================== //=================================以下为出错部分======================================= private void Thread_Load(object sender, EventArgs e) { Thread Add = new Thread(new ThreadStart(Add)); /*错误1 “Add”是“变量”,但此处被当做“方法”来使用 错误 2 “Thread.Thread”不包含采用“1”个参数的构造函数 */ Thread Div = new Thread(new ThreadStart(Div)); /*错误3 “Div”是“变量”,但此处被当做“方法”来使用 错误 4 “Thread.Thread”不包含采用“1”个参数的构造函数 */ } private void Start_Click(object sender, EventArgs e) { try { Add.Start();//错误5 “Thread.Thread.Add()”是一个“方法”,这在给定的上下文中无效 Div.Start();//错误6 “Thread.Thread.Div()”是一个“方法”,这在给定的上下文中无效 } catch (ThreadStateException) { Console.WriteLine("The thread has alread been startsd."); } } //=====================================以上为出错部分=================================== } }第一次使用多线程,请高手帮帮忙,改正错误。
谢谢!