日期:2014-05-18  浏览次数:20917 次

什么是异步?与多线程的区别?
异步和多线程应该是两个概念吧。
有个多线程的例子,如下:  
C# code

public DDD aaa;

            bbb = new Thread(new ThreadStart(aaa.ccc));
            bbb.Start();
    然后在类DDD中写的ccc方法
public class DDD
    {
 public void ccc()
        {
          //todo
        }
    }



希望高手举个类似如上代码的异步的例子?
感谢!!感谢!!

------解决方案--------------------
主线程和bbb线程就是异步的
------解决方案--------------------
Socket.BeginReceive 就异步了
------解决方案--------------------
多线程可以制造异步的效果.

有阻塞(不干活,白白等待)就需要异步 无阻塞无需异步,无需多线程.
------解决方案--------------------
C# code

public class UdpState 
    { 
        public UdpClient udpClient; 
        public IPEndPoint ipEndPoint; 
        public const int BufferSize = 1024; 
        public byte[] buffer = new byte[BufferSize]; 
        public int counter = 0; 
    } 
    public class AsyncUdpSever 
    { 
        private IPEndPoint ipEndPoint = null; 
        private IPEndPoint remoteEP = null; 
        private UdpClient udpReceive = null; 
        private UdpClient udpSend = null; 
        private const int listenPort = 1100; 
        private const int remotePort = 1101; 
        UdpState udpReceiveState = null; 
        UdpState udpSendState = null; 
        private ManualResetEvent sendDone = new ManualResetEvent(false); 
        private ManualResetEvent receiveDone = new ManualResetEvent(false); 
        public AsyncUdpSever() 
        { 
            ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort); 
            remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort); 
            udpReceive = new UdpClient(ipEndPoint); 
            udpSend = new UdpClient(); 
            udpReceiveState = new UdpState();            
            udpReceiveState.udpClient = udpReceive; 
            udpReceiveState.ipEndPoint = ipEndPoint; 

            udpSendState = new UdpState(); 
            udpSendState.udpClient = udpSend; 
            udpSendState.ipEndPoint = remoteEP; 
        } 
        public void ReceiveMsg() 
        { 
            Console.WriteLine("listening for messages"); 
            while(true) 
            { 
                lock (this) 
                {  
                    IAsyncResult iar = udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState); 
                    receiveDone.WaitOne(); 
                    Thread.Sleep(100); 
                } 
            } 
        } 
        private void ReceiveCallback(IAsyncResult iar) 
        { 
            UdpState udpReceiveState = iar.AsyncState as UdpState; 
            if (iar.IsCompleted) 
            { 
                Byte[] receiveBytes = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint); 
                string receiveString = Encoding.ASCII.GetString(receiveBytes); 
                Console.WriteLine("Received: {0}", receiveString); 
                //Thread.Sleep(100); 
                receiveDone.Set(); 
                SendMsg(); 
            } 
        } 

        private void SendMsg() 
        { 
            udpSend.Connect(udpSendState.ipEndPoint); 
            udpSendState.udpClient = udpSend; 
            udpSendState.counter ++; 

            string message = string.Format("第{0}个UDP请求处理完成!",udpSendState.counter); 
            Byte[] sendBytes = Encoding.Unicode.GetBytes(message); 
            udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState); 
            sendDone.WaitOne(); 
        } 
        private void SendCallback(IAsyncResult iar) 
        { 
            UdpState udpState = iar.AsyncState as UdpState; 
            Console.WriteLine("第{0}个请求处理完毕!", udpStat