日期:2014-05-18 浏览次数:21252 次
bool goon = true; //是否继续执行随后的程序 public void Main() { Thread staThread = new Thread(new ThreadStart(GetData())); //GetData() 是一个 staThread.SetApartmentState(ApartmentState.STA); staThread.Start(); //下面代码的意图:希望等这个GetData() 执行结束后再执行Excute()方法 while (staThread.ThreadState == ThreadState.Running) { goon = false; //按我的想法,GetData()未执行完前 应该是在这儿一直循环 } goon = true; if(goon )Excute(); / } //这是一个调用SAP接口取数的方法 public void GetData() { //code... } public void Excute() { //code... }
class Program { static Object obj = new Object(); static void Main(string[] args) { Thread staThread = new Thread(new ThreadStart(GetData)); staThread.SetApartmentState(ApartmentState.STA); staThread.Start(); Excute(); Console.Read(); } static void GetData() { Monitor.Enter(obj); Console.WriteLine("Get Data..."); Thread.Sleep(5000); Console.WriteLine("Geted Data"); Monitor.Pulse(obj); Monitor.Exit(obj); } static void Excute() { Monitor.Enter(obj); Monitor.Wait(obj); Console.WriteLine("Excuting..."); Thread.Sleep(1000); Console.WriteLine("Excuted"); Monitor.Exit(obj); } }