这应该很容易,但我找不到合适的术语来搜索......好吧,我是 C# 新手,我正在尝试制作一个简单的应用程序来编写 web 服务的返回。
我遇到了使用线程的需要......将参数传递给线程相当容易,我找不到从 Threaded 方法返回并更新我的 UI 以显示结果的方法(实际上没有真正的结果现在)
事件:
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
minhaSigla = Sigla.Text;
Task.Factory.StartNew(() => GetQuoteAndUpdateText(minhaSigla));
tb1.Text = "UIElement-TO-UPDATE";
}
然后是 Threaded 方法
private string GetQuoteAndUpdateText(string sign)
{
string SoapEnvelope = "";
SoapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
SoapEnvelope += "<soap:Envelope ";
SoapEnvelope += "xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" ";
SoapEnvelope += "xmlns:xsd= \"http://www.w3.org/2001/XMLSchema\" ";
SoapEnvelope += "xmlns:soap= \"http://schemas.xmlsoap.org/soap/envelope/\">";
SoapEnvelope += "<soap:Body>";
SoapEnvelope += " <GetQuote xmlns=\"http://www.webserviceX.NET/\"> ";
SoapEnvelope += " <symbol>" + sign + "</symbol> ";
SoapEnvelope += " </GetQuote> ";
SoapEnvelope += "</soap:Body>";
SoapEnvelope += "</soap:Envelope>";
EndpointAddress endpoint = new EndpointAddress("http://www.webservicex.net/stockquote.asmx");
BasicHttpBinding basicbinding = new BasicHttpBinding();
basicbinding.SendTimeout = new TimeSpan(3000000000);
basicbinding.OpenTimeout = new TimeSpan(3000000000);
stockbyname.StockQuoteSoapClient sbn = new stockbyname.StockQuoteSoapClient(basicbinding, endpoint);
XmlDocument xmlDocument = new XmlDocument();
return sbn.GetQuote(SoapEnvelope);
}
任何信息都将不胜感激,甚至评论我的代码有多糟糕:P