背景
我正在建立一個遠程c#應用程序來連接通過php服務器。它檢查客戶端程序是否被服務器識別,然後循環發送諸如在線時間等日誌並檢查命令。我隱約地評論了一些你可能不明白的原因,我爲什麼要這樣做。WebClient卡住了?
namespace Revamped_SERVER
{
class Program
{
static readonly String BASEURL = "http://mysite.com/index.php?";
static String OLD_ID = default(String);
static String CURRENT_ID = default(String);
static string GET(string PHP,Boolean IS_COMMAND)
{
using (WebClient CLIENT = new WebClient())
{
string RAW = CLIENT.DownloadString(BASEURL + PHP);
if (IS_COMMAND)
{
//this is a command, I remove the unique command ID so that previous commands and new ones aren't repeated if they have been submited more than once
CURRENT_ID = RAW.Remove(5, RAW.Length - 5);
return RAW.Remove(RAW.Length - 154, 154).Replace(CURRENT_ID, "");
}
else { return RAW.Remove(RAW.Length - 154, 154); } //this part removes extra string from server analytics a pain in the ***.
}
}
static string TIMESTAMP()
{
return DateTime.Now.ToString("M/d/yyy") + " - " + DateTime.Now.ToString("HH:mm:ss tt"); //Time to send to server
}
static void Main(string[] args)
{
using (WebClient CLIENT = new WebClient())
{
Boolean CONNECTED = false;
String Username = Environment.UserName;
while (true)
{
while (CONNECTED == false)
{
if (GET("REQUEST=ONLINE",false) == "TRUE")
{
CONNECTED = true;
}
}
if (CONNECTED)
{
if (GET("REQUEST=CHECK",false) == "FALSE")
{
CLIENT.OpenRead(BASEURL + "REQUEST=ADD");
}
CLIENT.OpenRead(BASEURL + "DATA=" + Username + "\r\nLast online at " + TIMESTAMP());
try
{
//this is where it gets stuck
String COMMAND = GET("REQUEST=COMMAND",true);
if (CURRENT_ID != OLD_ID)
{
OLD_ID = CURRENT_ID; //Set new ID
MessageBox.Show(COMMAND); //Show what Commands was sent by server
}
}
catch
{
CONNECTED = false;
}
}
}
}
}
}
}
問題
在第一循環中,它的工作原理非常清楚:連接,檢查它是否被該服務器識別,然後命令檢查。然而,因爲它再次循環發送日誌,但撐停留在:
String COMMAND = GET("REQUEST=COMMAND",true);
它給了我沒有錯誤,但是當我將鼠標懸停我的Web客戶端的鼠標它給了我這樣的:
我看過這篇文章,但並沒有向我解釋我如何避免這種情況。
http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx
有什麼不對的Web客戶端粘在第二循環?
詳細
- 我使用.NET 4.5
- 的Windows 7 64位
- 視覺工作室2012
- 服務器聯機,可以正常
DownloadString可能需要很長時間。如果不部署發行版二進制文件,您可能會在調試時看到更好的信息。暫時用調試二進制文件替換二進制文件,然後重試。 – 2013-03-02 16:56:46