request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
           allDone.WaitOne();在allDone.WaitOne();执行后就一直等待然后我想实现同步怎么做呀

解决方案 »

  1.   

    public class Http
        {
            private  ManualResetEvent allDone = new ManualResetEvent(false);
            private string sendData;               public delegate void CallBack(string url);
            public event CallBack CallBackEvent;        //post方式
            public void PostRequest(string url, string dataStr)
            {
                this.sendData = dataStr;
                Uri endpoint = new Uri(url);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
                allDone.WaitOne();        }        void RequestReady(IAsyncResult asyncResult)
            {
                HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
                Stream requestStream = request.EndGetRequestStream(asyncResult);
                StreamWriter sw = new StreamWriter(requestStream);
                byte[] byteArray = Encoding.Unicode.GetBytes(sendData);
                sw.Write(sendData);
                requestStream.Write(byteArray, 0, sendData.Length);
                sw.Close();
                request.BeginGetResponse(new AsyncCallback(ResponseReady), request);        }
            
            void ResponseReady(IAsyncResult asyncResult)
            {
                HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                try 
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(responseStream);
                        string retureValue = reader.ReadToEnd();
                        if (CallBackEvent != null)
                        {
                            CallBackEvent(retureValue);
                        }
                        reader.Close();
                    }
                }
                catch(Exception)
                {            }
                finally
                {
                     allDone.Set();            }
        }
    这个是个类,我在外面使用的类的PostRequest时候到上面那句就一直等待
    怎么回事呀,如何实现异步中的同步呀
      

  2.   

    silverlight 不支持同步请求。
    只能异步