注意 Visual Studio .NET 中的智能感知功能会显示 BeginInvoke和 EndInvoke的参数。如果您没有使用 Visual Studio 或类似的工具,或者您使用的是 C# 和 Visual Studio .NET,请参见异步方法签名获取有关运行库为这些方法定义的参数的描述。
using System; using System.Threading; publicclass AsyncDemo { // The method to be executed asynchronously. // publicstring TestMethod(int callDuration, outint threadId) { Console.WriteLine("Test method begins."); Thread.Sleep(callDuration); threadId = AppDomain.GetCurrentThreadId(); return"MyCallTime was "+ callDuration.ToString(); } } // The delegate must have the same signature as the method // you want to call asynchronously. publicdelegatestring AsyncDelegate(int callDuration, outint threadId);
using System; using System.Threading; publicclass AsyncDemo { // The method to be executed asynchronously. // publicstring TestMethod(int callDuration, outint threadId) { Console.WriteLine("Test method begins."); Thread.Sleep(callDuration); threadId = AppDomain.GetCurrentThreadId(); return"MyCallTime was "+ callDuration.ToString(); } } // The delegate must have the same signature as the method // you want to call asynchronously. publicdelegatestring AsyncDelegate(int callDuration, outint threadId);
publicclass AsyncMain { staticvoid Main(string[] args) { // The asynchronous method puts the thread id here. int threadId; // Create an instance of the test class. AsyncDemo ad =new AsyncDemo(); // Create the delegate. AsyncDelegate dlgt =new AsyncDelegate(ad.TestMethod); // Initiate the asychronous call. IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, null, null); Thread.Sleep(0); Console.WriteLine("Main thread {0} does some work.", AppDomain.GetCurrentThreadId()); // Call EndInvoke to Wait for the asynchronous call to complete, // and to retrieve the results. string ret = dlgt.EndInvoke(out threadId, ar); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret); } }
publicclass AsyncMain { staticvoid Main(string[] args) { // The asynchronous method puts the thread id here. int threadId; // Create an instance of the test class. AsyncDemo ad =new AsyncDemo(); // Create the delegate. AsyncDelegate dlgt =new AsyncDelegate(ad.TestMethod); // Initiate the asychronous call. IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, null, null); Thread.Sleep(0); Console.WriteLine("Main thread {0} does some work.", AppDomain.GetCurrentThreadId()); // Wait for the WaitHandle to become signaled. ar.AsyncWaitHandle.WaitOne(); // Perform additional processing here. // Call EndInvoke to retrieve the results. string ret = dlgt.EndInvoke(out threadId, ar); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret); } }
publicclass AsyncMain { staticvoid Main(string[] args) { // The asynchronous method puts the thread id here. int threadId; // Create an instance of the test class. AsyncDemo ad =new AsyncDemo(); // Create the delegate. AsyncDelegate dlgt =new AsyncDelegate(ad.TestMethod); // Initiate the asychronous call. IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, null, null); // Poll while simulating work. while(ar.IsCompleted ==false) { Thread.Sleep(10); } // Call EndInvoke to retrieve the results. string ret = dlgt.EndInvoke(out threadId, ar); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret); } }
publicclass AsyncMain { // Asynchronous method puts the thread id here. privatestaticint threadId; staticvoid Main(string[] args) { // Create an instance of the test class. AsyncDemo ad =new AsyncDemo(); // Create the delegate. AsyncDelegate dlgt =new AsyncDelegate(ad.TestMethod); // Initiate the asychronous call. Include an AsyncCallback // delegate representing the callback method, and the data // needed to call EndInvoke. IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, new AsyncCallback(CallbackMethod), dlgt ); Console.WriteLine("Press Enter to close application."); Console.ReadLine(); } // Callback method must have the same signature as the // AsyncCallback delegate. staticvoid CallbackMethod(IAsyncResult ar) { // Retrieve the delegate. AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState; // Call EndInvoke to retrieve the results. string ret = dlgt.EndInvoke(out threadId, ar); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret); } }
posted on
2004-11-03 16:36团团ta爸
阅读(4121)
评论(0)
收藏举报
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】园子的不务正业:向创业开发者推荐「楼盘」- 杭州云谷中心
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】天翼云爆款云主机2核2G限时秒杀,28.8元/年起!立即抢购