C#用for循环生成线程,生成的线程对函数A执行操作。等执行完函数A之后,终止该线程,然后继续for循环,再生成一个线程,再对函数A进行操作,等执行完函数A之后,终止该线程,。继续执行for循环,直至for 循环结束,函数A不能加静态属性。线程必须生成、终止,再生成、在终止。这个该怎么写呢?求教高手急寻高手,这个程序我写了好几天了,也没有写出来。哪位高手会,帮下小弟,最好给个例子
调试欢乐多
{
var t=new Thread(A);
t.Start();
t.Join();
}
不知道你这样做有什么特殊的意义
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void button1_Click(object sender, EventArgs e)
{
for (Int32 i = 0; i < 1000; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(Foo));
t.IsBackground = true;
t.Start(i);
t.Join();
}
} private void Foo(Object o)
{
this.Text = "Thread " + o.ToString();
Thread.Sleep(3000);
}
private void Foo(Object o)
{
lock(this)
{
this.Text = "Thread " + o.ToString();
Thread.Sleep(3000);
}
}
{
static EventWaitHandle _workEvent = new ManualResetEvent(false);
public static void Main()
{
myThread t = new myThread(); while (true)
{
string s = Console.ReadLine();
t.Run(s);
}
}
} class myThread
{
Thread _thread;
object _state;
EventWaitHandle _workEvent; public myThread()
{
_workEvent = new ManualResetEvent(false); _thread = new Thread(new ThreadStart(invoke));
_thread.IsBackground = true;
_thread.Start();
} void invoke()
{
while (_workEvent.WaitOne())
{
A();
_workEvent.Reset();
}
} public bool IsBusy
{
get
{
return _workEvent.WaitOne(0);
}
} public void Run(object state)
{
if (!IsBusy)
{
_state = state;
_workEvent.Set();
}
} public void A()
{
Console.WriteLine(string.Format("接收到输入信息:{0}", _state));
}
}
{
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(SendMessage));
thread.Start();
}
public void SendMessage()
{}