請隨意創建一個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