背景
我正在构建一个远程 c# 应用程序以通过 php 服务器进行连接。它检查客户端程序是否被服务器识别,然后循环发送日志,例如在线时间并检查命令。我模糊地评论了一些你可能不明白我为什么这样做的部分。
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
这个 WebClient 卡在第二个循环有什么问题?
细节
- 我正在使用 .NET 4.5
- Windows 7 64 位
- 视觉工作室 2012
- 服务器在线并且运行良好