请随意创建一个Windows窗体应用程序。重现错误禁用网络连接并运行代码。它会在每1秒后尝试重新连接。在4-5次尝试启用网络连接并进入调试模式后,您会注意到即使提取产品,Reconnect()方法也被调用4-5次。一旦产品被提取,为什么它会一次又一次地调用Reconnect()方法?Winform递归循环 - 一次又一次调用方法
string apiUrl = "https://api.gdax.com/products";
string json;
private void Form1_Load(object sender, EventArgs e)
{
try
{
if (FillProducts()) // product need first
{
}
}
catch (WebException ex)
{
ReconnectOnError(ex);
}
}
private bool FillProducts()
{
bool isDone = false;
try
{
json = GetGDXJSONData(apiUrl);
JsonSerializer serializer = new JsonSerializer();
DataTable dt = (System.Data.DataTable)Newtonsoft.Json.JsonConvert.DeserializeObject(json, (typeof(System.Data.DataTable)));
count = dt.Rows.Count;
if (count > 0)
isDone = true;
}
catch (Exception ex)
{
isDone = false;
ReconnectOnError(ex);
}
return isDone;
}
int count = 0;
private void ReconnectOnError(Exception errorMessage)
{
try
{
Thread.Sleep(1000);
if (count < 1)
{
FillProducts(); // it comes on this point again and again even the count is greater than 1
Reconnect();
}
else
{
Reconnect();
}
}
catch (WebException ex)
{
ReconnectOnError(ex);
}
}
private void Reconnect()
{
// why this is called the number of times the attempt was made to fill the products?
}
private string GetGDXJSONData(string apiUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.ContentType = "application/json";
request.UserAgent = "gdax-node-client";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
}
编辑 - 这是一个示例代码和我做很多的重新连接()方法的事情,一旦产品有牵强。 另外,如果错误发生在产品被提取后,则不要获得产品,只需调用Reconnect()方法,这就是其他原因。
编辑2 - 请不要仅仅通过查看代码来回复。如果您创建了一个表单并自己运行,并且可以自己查看错误,请告知如何解决此问题。
更新 - 我知道我进入了无限次迭代。我想这和现在的作品:
string apiUrl = "https://api.gdax.com/products";
string json;
private void Form1_Load(object sender, EventArgs e)
{
if (FillProducts()) // product need first
{
}
}
bool isOk = false;
private string GetGDAXProducts()
{
try
{
json = GetGDXJSONData(apiUrl);
return json;
}
catch (Exception ex)
{
return "-1";
}
}
int count = 0;
private bool FillProducts()
{
bool isDone = false;
string retVal = GetGDAXProducts();
while (retVal == "-1")
{
retVal = GetGDAXProducts();
}
if (retVal != "-1")
{
JsonSerializer serializer = new JsonSerializer();
DataTable dt = (System.Data.DataTable)Newtonsoft.Json.JsonConvert.DeserializeObject(json, (typeof(System.Data.DataTable)));
count = dt.Rows.Count;
if (count > 0)
isDone = true;
}
return isDone;
}
private string GetGDXJSONData(string apiUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.ContentType = "application/json";
request.UserAgent = "gdax-node-client";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
其归因于其他部分'如果(计数<0)' –
再次fillproducts后调用它... – Proxytype
我不得不这样做。它不是在该行未来虽然。如果你运行代码,你会看到它调用FillProducts();重新连接();一次又一次,即当没有互联网时第一次尝试重新连接的次数 – user1254053