如何异步调用 Visual C# 方法

来源:互联网 发布:淘宝网频现违规刀具 编辑:程序博客网 时间:2024/05/16 18:34

 

using System;
using System.Threading ; 
using System.Windows.Forms ;

namespace AsyncDemo
{
    
/// <summary>
    
/// 如何异步调用 Visual C# 方法
    
/// 出自:http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B315582
    
/// </summary>

    class AsyncDemo
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            AsyncDemo ad 
= new AsyncDemo () ;
            
//ad.DemoSyncCall() ;
            
//ad.DemoEndInvoke();
            
//ad.DemoWaitHandle();
            
//ad.DemoPolling();
            ad.DemoCallback();
            Console.Read();
        }


        
string LongRunningMethod (int iCallTime, out int iExecThread)
        
{
            Thread.Sleep (iCallTime) ;
            iExecThread 
= AppDomain.GetCurrentThreadId ();
            
return "MyCallTime was " + iCallTime.ToString() ;
        }


        
delegate string MethodDelegate(int iCallTime, out int iExecThread)  ;

        
示例 1: 同步调用方法

        
示例 2: 通过 EndInvoke() 调用模式异步调用方法

        
示例 3: 异步调用方法并使用 A WaitHandle 来等待调用完成

        
示例 4: 异步调用方法通过轮询调用模式

        
示例 5: 异步方法完成后执行回调
    }

}

原创粉丝点击