none
关于带超时功能webclient伪同步发送数据的方法疑惑,总是报异常 RRS feed

  • 问题

  • ManualResetEvent _clientDone = new ManualResetEvent(false);

    WebClient _webClient;

    public string HttpSend(Uri address, string method, string data)
            {
                string response = "Operation Timeout";

                _webClient.UploadStringCompleted += (sender, e) =>
                {
                    Debug.WriteLine("called");

                    if (e.Error != null)
                    {
                        response = e.Error.Message;
                    }
                    if (e.Cancelled)
                    {
                        response = "Operation Timeout";
                    }
                    else if(e.Result != null)
                    {
                        response = e.Result;
                    }

                    _clientDone.Set();               
                };

                _clientDone.Reset();

                _webClient.UploadStringAsync(address, method, data);

                _clientDone.WaitOne(TIMEOUT);

                if (_webClient.IsBusy)
                {
                    _webClient.CancelAsync();
                }

                return response;
            }

    当自定义的超时时间已到并且事件没有触发时会调用cancel方法,这是会直接进入完成事件,但事件方法走到最后总会报异常,不是代码本身报异常,是委托事件结束的时候自动报的异常,一直没发现原因,望达人相助!附带异常信息:

    未处理 System.Net.WebException
      Message=WebException
      StackTrace:
           at System.Net.Browser.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
           at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
           at System.Net.WebClient.<>c__DisplayClass1.<UploadDownloadBits>b__0(Byte[] returnBytes, Exception exception, AsyncOperation asyncOp2)
           at System.Net.WebClient.UploadBitsRequestCallback(IAsyncResult result)
           at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass6.<InvokeGetRequestStreamCallback>b__4(Object state2)
           at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadPool.WorkItem.doWork(Object o)
           at System.Threading.Timer.ring()
      InnerException: System.Net.WebException
           Message=WebException
           StackTrace:
                at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
                at System.Net.Browser.AsyncHelper.<>c__DisplayClassa.<BeginOnUI>b__6(Object sendState)
                at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
                at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
                at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
                at System.Delegate.DynamicInvokeOne(Object[] args)
                at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
                at System.Delegate.DynamicInvoke(Object[] args)
                at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
                at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
                at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
                at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
                at System.Delegate.DynamicInvokeOne(Object[] args)
                at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
                at System.Delegate.DynamicInvoke(Object[] args)
                at System.Windows.Threading.DispatcherOperation.Invoke()
                at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
                at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
                at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
                at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
                at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)


    2012年6月29日 0:36

全部回复