K_X
K_X

Reputation: 173

WebClient gets stuck?

Background
I am building a remote c# application to connect via a php server. It checks if the client program is recognized by the server and then loops to send logs such as time online and checks for commands. I have vaguely commented some parts you may not understand why I did them.

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; 
                            }
                        }
                    }
                }
            }
        }
    }


Problem
On the first loop, it works perfectly well: Connects, checks if it's recognised by the server and then checks for commands. However, as it loops again it sends logs but stays stuck at:

String COMMAND = GET("REQUEST=COMMAND",true);

It gives me no errors but when I hover my mouse of the web client it gives me this:

Cannot evaluate expression because the code of the current method is optimized.

I've had a look at this articles but it doesn't explain to me how I can avoid this in my case.

http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx

What's wrong with this WebClient being stuck on its second loop?


Details

Upvotes: 1

Views: 1341

Answers (1)

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

WebClient.OpenRead returns a stream, that needs to be passed to a stream reader to read the response.

In your case, you are not using the stream returned at all. Since you are reusing the instance of webclient, my guess is this is what is causing the behaviour you mention.

Upvotes: 1

Related Questions