好的,所以基本上我有一个返回字符串的函数,但是要获取该字符串,它使用 webrequest 这意味着当它执行该 webrequest 时,表单将被锁定,除非我将它放在不同的线程中。
但是我想不出一种方法来捕获线程中返回的数据,因为它是使用 thread.start 开始的,这是一个无效的。
请问有什么帮助吗?
当前代码(如果对任何人都重要):
string CreateReqThread(string UrlReq)
{
System.Threading.Thread NewThread = new System.Threading.Thread(() => CreateReq(UrlReq));
string ReturnedData = "";
return ReturnedData;
}
string CreateReq(string url)
{
try
{
WebRequest SendReq = WebRequest.Create(url);
SendReq.Credentials = CredentialCache.DefaultCredentials;
SendReq.Proxy = WebRequest.DefaultWebProxy; //For closed port networks like colleges
SendReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
SendReq.Timeout = 15000;
System.IO.StreamReader Reader = new System.IO.StreamReader(SendReq.GetResponse().GetResponseStream());
string Response = Reader.ReadToEnd();
Reader.Close();
return Response;
}
catch (WebException e)
{
EBox(e.Message, "Unknown Error While Connecting");
return null;
}
}