public static void Run() { AsyncDemo demo = new AsyncDemo("jiangnii"); // Execute begin method IAsyncResult ar = demo.BeginRun(null, null); // You can do other things here // Use AsyncWaitHandle.WaitOne method to block thread for 1 second at most ar.AsyncWaitHandle.WaitOne(1000, false); if (ar.IsCompleted) { // Still need use end method to get result, // but this time it will return immediately string demoName = demo.EndRun(ar); Console.WriteLine(demoName); } else { Console.WriteLine("Sorry, can't get demoName, the time is over"); } }
public static void Run2() { AsyncDemo demo = new AsyncDemo("jiangnii"); // Execute begin method IAsyncResult ar = demo.BeginRun(null, null); Console.Write("Waiting.."); while (!ar.IsCompleted) { Console.Write("."); // You can do other things here } Console.WriteLine(); // Still need use end method to get result, //but this time it will return immediately string demoName = demo.EndRun(ar); Console.WriteLine(demoName); }