首页 \ 问答 \ 使用await GetResponseAsync()时出现死锁(Deadlock when using await GetResponseAsync())

使用await GetResponseAsync()时出现死锁(Deadlock when using await GetResponseAsync())

我已经使用visual studio 2015中的控制台应用程序成功将json数据拉入字符串。现在我已经尝试复制这个用于wpf,但我发现我不能使用.GetResponse()而且我必须使用GetResponseAsync ()代替。 谷歌搜索了一段时间后,我想我理解async / await的观点,但我似乎陷入了僵局。 所以这是我目前的代码:

这是从Web API提取返回数据并返回JSON字符串的异步方法。 或者在本例中为Task<String>

public async static Task<String> GetTrola(string station)
        {
            var url = "http://www.trola.si/" + station;

            string text;

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Accept = "application/json";

            var answer = (HttpWebResponse)(await request.GetResponseAsync());

            using (var sr = new StreamReader(answer.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
            return text;
        }

然后我在按钮单击事件中调用该函数,如下所示:

string station = textBox.Text.ToString();
string output = GetJson.Trola.GetTrola(station).Result;
var myObject = JsonConvert.DeserializeObject<Result>(output);

但我认为问题出在GetData函数中,特别是这一行:

var answer = (HttpWebResponse)(await request.GetResponseAsync());

我不知道为什么会发生这种情况,因为我等待使用await关键字的请求? 我还缺少别人的东西?


I've successfully pulled json data into a string using a console application in visual studio 2015. Now I've tried to replicate this for wpf, but I've found I can't use .GetResponse() and I have to use GetResponseAsync() instead. After googling for a while I think I understand the point of async/await but I seem to be stuck in a deadlock. So here is my current code:

This is the async method that pulls the return data from a web API and returns a JSON string. Or in this case a Task<String>.

public async static Task<String> GetTrola(string station)
        {
            var url = "http://www.trola.si/" + station;

            string text;

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Accept = "application/json";

            var answer = (HttpWebResponse)(await request.GetResponseAsync());

            using (var sr = new StreamReader(answer.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
            return text;
        }

I then call the function in a button click event, like this:

string station = textBox.Text.ToString();
string output = GetJson.Trola.GetTrola(station).Result;
var myObject = JsonConvert.DeserializeObject<Result>(output);

But I think the problem is in the GetData function in particular this line:

var answer = (HttpWebResponse)(await request.GetResponseAsync());

I don't know why it happens, since i awaited the request with the await keyword? Something else I'm missing?


原文:https://stackoverflow.com/questions/34466272
更新时间:2023-09-23 19:09

最满意答案

/div/text()

从给出的示例中,这个XPath将获得div元素下面的所有文本节点,在本例中为test2。

如果您可以详细说明问题,我们可以更好地帮助您。 该分区包含3个孩子:span元素,文本节点和ab元素。 span和b每个都有一个文本节点子节点。 使用XPath,您可以只选择元素(/ div / *),仅选择文本节点(/ div / text())或所有节点类型(/ div / node())。

编辑:/ text()只返回根级文本节点。 在这种情况下,我希望它返回一个包含3个文本节点的节点列表:

\t\t\t\t\t    \r\n\t\t 
this is the text I want
\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t

您是否只在结果节点列表中选择第一个节点? 有几个格式良好的问题,比如你的应该可能<br/>


/div/text()

From the example given, this XPath will get you all text nodes underneath the div element, in this case test2.

If you could elaborate more on the question we might better be able to help you. The Div contains 3 children: a span element, a text node and a b element. The span and b each have a text node child. Using XPath you could select elements only (/div/*), text nodes only (/div/text()) or all node types (/div/node()).

EDIT: /text() will only return you root level text nodes. In this case I would expect it to return a node list containing 3 text nodes:

\t\t\t\t\t    \r\n\t\t 
this is the text I want
\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t

Are you perhaps only selecting the first node in the resultant node list? There are a few issues of well-formedness such as your <br> should probably be <br/>.

相关问答

更多
  • 例如,如果您的XML看起来像Bar那么“Bar”实际上被认为是一个单独的节点: XmlText节点(从XmlNode分类)。 该XmlText节点的Value属性将为“Bar”。 “Foo”被认为是XmlElement (也从XmlNode分类)。 XmlNode.Value根据节点的类型返回不同的东西。 请参见此表 ,其中表示Value始终为Element节点返回null 。 Foo节点的InnerText返回“Bar”,因为它连接子节点的值(在这种情况下,只有一个XmlText节点 ...
  • 给这样的东西一个镜头。 您的帖子中缺少一些细节,因此我不得不做出一些假设。 有以下两种方法: 1)使用getElementByID并查看InnerText是否返回 2)使用getElementByID然后迭代段落标记。 Public Sub test() Dim ie As Object Dim vCompanyAddress As Variant Dim i As Long: i = 0 Dim Elements ...
  • 你正在努力做到这一点: var folio = Convert.ToString(vector["folio"]); Co.SetAttribute("folio", folio); 没有必要担心像InnerText这样的东西。 You are doing this the hard way: var folio = Convert.ToString(vector["folio"]); Co.SetAttribute("folio", folio); There is no need to worry ...
  • 如果您有URL,请使用Load()方法,如果您有html页面,请使用LoadHtml() 。 在这种情况下,您似乎需要第一种方法: htmlDoc.Load(htmlWeb) 正如您所说的那样,要获取body标签,您可以使用这个简单的XPath //body 更新: 我错过了HAP的HtmlDocument与XDocument不同,无法直接从URL Load的事实。 该方法仅接受本地计算机中文件的路径。 要从URL加载HtmlDocument ,您需要使用HtmlWeb的Load()方法。 试试这种方式: ...
  • 你试了吗? //myparent/mychild[text() = 'foo'] 或者,您可以使用self轴的快捷方式: //myparent/mychild[. = 'foo'] Have you tried this? //myparent/mychild[text() = 'foo'] Alternatively, you can use the shortcut for the self axis: //myparent/mychild[. = 'foo']
  • 更改控件上的选择时,所选属性不会更新。 它用于在首次显示控件时定义所选项目(默认选项)。 要获取当前选择,您应调用selectedIndex方法以找出选择的项目。 HtmlElement element = webBrowser1.Document.GetElementById("F8"); object objElement = element.DomElement; object objSelectedIndex = objElement.GetType().InvokeMember("selected ...
  • 我会重构你的代码。 您已经在HTML中获得了行的ID,所以让我们使用它。 TestBuilder往往使事情变得更难以阅读,并且包含太多属性以使搜索变得高效或有意义。 因此,您的.uitest文件有两个子类:.designer.cs(上面的代码所在的位置)和.cs文件。 打开.cs文件并通过执行以下操作找到您的行: public HtmlDiv gridRow(int index) { HtmlDiv target = new HtmlDiv(browser); target.Search ...
  • 这应该做到这一点 XmlDocument xml=new XmlDocument(); xml.Load(path); string OrderId = "1262"; string ReadedBarcode = "1234"; XmlNodeList BarCodeNodeList = xml.SelectNodes("//Order[@Id='" + OrderId + "']"+"//Product/Barcode[t ...
  • /div/text() 从给出的示例中,这个XPath将获得div元素下面的所有文本节点,在本例中为test2。 如果您可以详细说明问题,我们可以更好地帮助您。 该分区包含3个孩子:span元素,文本节点和ab元素。 span和b每个都有一个文本节点子节点。 使用XPath,您可以只选择元素(/ div / *),仅选择文本节点(/ div / text())或所有节点类型(/ div / node())。 编辑:/ text()只返回根级文本节点。 在这种情况下,我希望它返回一个包含3个文本节点的节点列 ...
  • 使用\K放弃先前在匹配时打印的匹配字符。 \ K保持文本匹配到目前为止整个正则表达式匹配。 \/path\/\d+">\K(?!.*img).*?(?=<\/a>) DEMO $re = "~\/path\/\d+\">\K(?!.*img).*?(?=<\/a>)~m"; $str = "Get Me\n\n

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。