简单的讲吧 假使我现在有这样两个函数
public static int test(int a)
        {            
            return ++a;
        }
static void Main(string[] args)
        {
            int a, b;
            a = 1;
            b = test(a);
            Console.WriteLine("{0}", b);
        }
假使test函数是web给的接口 我要把main函数与web的通讯改成异步的该怎么改?就是那个关于AsyncCallbsck函数的那种方式 请高人给讲解一下 或者直接给个改好的代码也行 

解决方案 »

  1.   

    socket-client
    ----------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace socket_client
    {
        //客户端
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false;
               
            }
            Socket s;//聊天用
            Thread th;        //连接
            private void button1_Click(object sender, EventArgs e)
            {
                //步骤1 配置远程服务器信息
                IPEndPoint removeServer = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
                //步骤2  创建套接字
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //步骤3 套接字连接远程服务器
                s.Connect(removeServer);
                //步骤4 提示连接状态
                if (s.Connected)
                {
                    label4.Text = "连接服务器成功!";                //步骤5 循环接收服务器发来的消息
                    th = new Thread(new ThreadStart(BB));
                    th.IsBackground = true;
                    th.Start();
                }
            }        void BB()
            {
                while (true)
                {
                    byte[] bb = new byte[1024];
                    int i= s.Receive(bb); //接收数据,返回每次接收的字节总数
                    string removeMsg = Encoding.Unicode.GetString(bb,0,i);
                    if (removeMsg == "CMD--EXIT")//收到的是退出通知
                    {
                        label4.Text = "无连接";
                        DialogResult re=MessageBox.Show("服务器已经关闭.\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);                    MessageBox.Show(re.ToString());
                        if (re == DialogResult.OK)
                        {
                            sendExit();//告诉服务器我退出了
                            Application.Exit();
                        }
                        return;
                    }
                    richTextBox1.AppendText(removeMsg+"\n") ;
                    richTextBox1.ScrollToCaret();
                   
                   
                }
            }
            //发送消息
            private void button3_Click(object sender, EventArgs e)
            {
                string msg = "客户端:" + richTextBox2.Text;
                byte[] bb = Encoding.Unicode.GetBytes(msg);
                s.Send(bb);
                richTextBox2.Text = "";
                richTextBox2.Focus();
            }        //发送“客户端退出提示”
            void sendExit()
            {
                string cmd = "CMD--EXIT";
                byte[] bb = Encoding.Unicode.GetBytes(cmd);
                s.Send(bb);
            }    }
    }
      

  2.   

    socket-server--------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace socket_server
    {
        //服务器
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false; //可以调试时,不捕捉控件创建线程错误
            }        Thread th;
            Socket s1;//监听用
            Socket s2;//聊天用
            //监听
            private void button1_Click(object sender, EventArgs e)
            {
               
                IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
                //步骤1 创建网络端点IPEndPoint
                IPEndPoint myServer = new IPEndPoint(ip, 888);
                //步骤2 创建套接字Socket
                s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //步骤3  套接字绑定到网络端点
                s1.Bind(myServer);
                label1.Text = ip+": 888  \n等待客户端连接......";            th = new Thread(new ThreadStart(AA));
                th.IsBackground = true;
                th.Start();
            }        void AA()
            {
                //步骤4  监听
                s1.Listen(5);
                //步骤5  接受客户端连接
                s2 = s1.Accept();
                //步骤6  判断连接状态
                if (s2.Connected)
                {
                    label1.Text = "已有客户端连接!";                //步骤7 循环接收客户端消息
                    while (true)
                    {
                        byte[] bb = new byte[1024];
                        int i= s2.Receive(bb);
                        string removeMsg = Encoding.Unicode.GetString(bb,0,i);
                        if (removeMsg == "CMD--EXIT")//收到的是退出通知
                        {
                            label1.Text = "客户端已经取消了连接";
                            return;
                        }
                        richTextBox1.AppendText( removeMsg+"\n" );
                        richTextBox1.ScrollToCaret();
                    }
                }        }        //停止监听
            private void button2_Click(object sender, EventArgs e)
            {
                sendExit();//告诉客户端
                s2.Shutdown(SocketShutdown.Both);
                s1.Close();
                th.Abort();
                label1.Text = "无连接";
                
            }        //发送消息
            private void button3_Click(object sender, EventArgs e)
            {
                string msg = "服务器:" + richTextBox2.Text;
                byte[] bb = Encoding.Unicode.GetBytes(msg);
                s2.Send(bb);
                richTextBox2.Text = "";
                richTextBox2.Focus();
            }        //发送“服务器退出提示”
            void sendExit()
            {
                string cmd = "CMD--EXIT";
                byte[] bb = Encoding.Unicode.GetBytes(cmd);
                s2.Send(bb);
            }
        }
      

  3.   


    [原创]C#调用WebService异步方法源代码下载
    http://www.vjsdn.net/bbs/bbsTopicDetails.aspx?pid=111071216[原创]C#调用WebService异步方法/WebService异步调用详解 
    http://www.vjsdn.net/bbs/bbsTopicDetails.aspx?pid=111071215
      

  4.   

    我意思是不用管网络发送方式 我就是想知道那个AsyncCallback委托的那个写法 不用管网络通讯 就假使都是本地的就行