c#异步执行方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinInterfaceCallBackVs08
{
    class Class1
    {
        public Class1()
        { 
            new Action<string>((s) => { }).BeginInvoke("", null, null);

            new Action<string>(exSql).BeginInvoke("", null, null);
        }
        public void exSql(string sql) { }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinInterfaceCallBackVs08
{
    class Class1
    {
        public Class1()
        { 
            new Action<string>((s) => { }).BeginInvoke("", null, null);
        }
    }
}

大括号中写方法名,双引号中写sql语句 以上方法适合.net 4.0

如果是.net 1.1则用一下方法

private void button1_Click(object sender, System.EventArgs e)
        {
            BookingMgr.BookingMgr sms=new BookingMgr.BookingMgr(); //同样的实例化对象
            sms.BeginRmkPnr(new AsyncCallback(MySMS),sms); //使用带Begin开头那个方法,要传一个方法进去(我这里叫MySMS)

            
        }
        //这个方法中实现真正的调用及结果!
        public static void MySMS(IAsyncResult iar)
        {
            BookingMgr.BookingMgr sms=new BookingMgr.BookingMgr(); //同样的实例化对象
            string sSendRes=sms.RmkPnr("","","");
        }

以上方法在.net1.1的B/S和C/S结构通用,在.net2.0的b/s结构也能用,但.net2.0的c/s结构下就用不了,可以用下面这种方法

 
         private void test()
        {
            localhost.WebService service 
= new localhost.WebService();
            service.HelloWorldCompleted 
+= new localhost.HelloWorldCompletedEventHandler(service_hello);
            service.HelloWorldAsync();
        }

        
private void service_hello(object sender, localhost.HelloWorldCompletedEventArgs e)
        {
            
if (e.Error == null)
                MessageBox.Show(e.Result);
            
else
                MessageBox.Show(e.Error.Message);
        }
原文地址:https://www.cnblogs.com/honghong75042/p/3095574.html