C# HTTPWebRequest Freezes the program !

07/11/2012 02:15 badguy4you#1
I created a small GET request using HTTPWEBREQUEST

Code:
            string URL = "http://google.com";
            HttpWebRequest GetRequest = (HttpWebRequest)WebRequest.Create(URL);
            GetRequest.Method = "GET";
            GetRequest.CookieContainer = Jar;
            GetRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
            GetRequest.KeepAlive = true;
           
            HttpWebResponse GetResponse = (HttpWebResponse)GetRequest.GetResponse();
            StreamReader GetReader = new StreamReader(GetResponse.GetResponseStream());
            richTextBox1.Text = GetReader.ReadToEnd();
But the program totally freezes while getting the response

so i tried to use asynchronous but i cant understand it so anyone here can tell me how could i convert the code above to async so it wont freeze the program
07/11/2012 02:43 tobindev#2
You need to do the HttpWebRequest in another thread. For that you can use a Backgroundworker.
07/11/2012 02:47 badguy4you#3
Will try and tell ya

I get this error !

Cross-thread operation not valid: Control 'select' accessed from a thread other than the thread it was created on.

i think cuz only the UI thread can access UI components

any workaround this
07/11/2012 02:57 tobindev#4
This happens when you perform work on the GUI in the Backgroundworker_DoWork part. You can finally report sth to the GUI in the Completed Event without problems but if you want to tell sth to GUI while working you need the ReportProgress Method.
07/11/2012 03:01 badguy4you#5
i want to update the gui from the background worker cuz my code just adds and reads text from a richtextbox so must be updated

and the website name is captured from a textbox
07/11/2012 03:07 MoepMeep#6
Thats basic c# knowledge, I allready told you to learn the language.
07/11/2012 03:08 tobindev#7
For this u need like I said the ReportProgress Method and the ProgressChanged Event.
07/11/2012 03:09 badguy4you#8
MoepMeep -> And i already told you dont replay to my thread if you will not say something useful (i am only asking in this cuz i never used the worker in any program) so IS THERE A HELL PROPLEM THAT I ASK IN SOMETHING HERE ? ofc no problem This is a community MEANS share what you got ( you may request HELP or give HELP )

When i can help someone i just help not just saying go learn to free myself
07/11/2012 14:25 Demon-777#9
You can do it like this (not tested):

1. Add new extension for control invoking from non-gui threads.
Code:
internal static class CrossInvoker
    {
        internal static void InvokeEx<T>(this T control, Action<T> action)
            where T : ISynchronizeInvoke
        {
            if (control.InvokeRequired)
                control.Invoke(action, new object[] { control });
            else
                action(control);
        }
    }
So now all your controls have InvokeEx function.

2. Then edit your WebRequest code (something like this):
Code:
internal void MakeRequest()
        {
            const string url = "http://www.google.com/";

            var request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = "GET";
            request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
            request.KeepAlive = true;

            var response = (HttpWebResponse) request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            
            // Here we set 'Text' property of txtInput.
            txtInput.InvokeEx(c => c.Text = reader.ReadToEnd());
        }
3. Then run new thread somewhere in your project: (and maybe wait till it's done)
Code:
new Thread(MakeRequest) { IsBackground = true }.Start();
07/12/2012 08:16 ЙôČhěάŧĪмPĞM-hacked#10
Nice thanks you :)