0

我有以下响应代码。当调用 Messagebox.sohw() 方法显示代码:200 时,我在 System.Windows.ni.dll 中发生了类似“System.UnauthorizedAccessException”类型的异常但未在用户代码错误中处理的错误。我的 Json 就像 {"meta":{"code":200}}

          private void ReadWebRequestCallback(IAsyncResult ar)
          {
           HttpWebRequest myReq = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myReq.EndGetResponse(ar);
uritext.Text = myResponse.ResponseUri.ToString();
            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                string results = httpwebStreamReader.ReadToEnd();    
            }
            myResponse.Close();
           var root = JsonConvert.DeserializeObject<RootObject>(jsondata);
           String codenum = root.meta.code.ToString();
           Messagebox.sohw(codenum);
           }
   public class Meta
    {
        public int code { get; set; }
    }

    public class RootObject
    {
        public Meta meta { get; set; }
    }
4

1 回答 1

2

问题是HttpWebRequest来自非 UI 线程的回调发生。

以下是您可以MessageBox.Show在 UI 线程上执行的操作:

Dispatcher.BeginInvoke(() => MessageBox.Show(codenum));
于 2013-02-07T08:49:42.113 回答