Request method and thread problem?

I am developing an application that removes a website for Google + 1 shares, Facebook and tweets. I have a request method that takes a url and then disconnects and selects an account for each type of social networks.

This logic is as follows: -

  • Take url
  • Fulfills requests through a local / standard IP address until we get a speed limit / non-500
  • On error
    • Call SelectNewProxy() , which iterates through the list of proxies and returns a random value (a good way to avoid access restrictions for your IP address).
    • Remove the bad proxy from the list to avoid re-selection.
    • Start a timer that increments every second.
  • When timer == 600 (10 minutes)
    • Create a new WebProxy and try again the requests for our local / standard IP address.
    • Reset timer

Rinse and repeat

The code is as follows:

 public string Request(string action) { HttpWebRequest req; OnStatusChange(new MyArgs() { Message = "Status: Requesting..." }); string response = string.Empty; while (response.Equals(string.Empty) && proxy != null) { try { req = (HttpWebRequest)WebRequest.Create(action); req.Proxy = proxy; HandleUIMessages(action, proxy); response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd(); } catch { //OnProxyChange(new MyArgs() { ProxyMessage = string.Format("Proxy: {0}", proxy.Address.ToString()) }); RemoveProxy(proxy); if (!timer.Enabled) { timer.Interval = (int)TimeInterval.OneSecond; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; timer.Start(); } WebProxy reset = new SelectNewProxy(); proxy = counter >= 600 ? reset : proxy = SelectNewProxy(); } } return response; } 

It is worth noting that I use ThreadPool , and each request runs its own thread in it. It seems that this will work, but I do not get the desired effect, the counter will reach “600” and set proxy = reset , but it seems to do it very briefly, perhaps only for the first thread that gets into it? Then timer_Elapsed is timer_Elapsed , and counter is reset. Could it be that the thread hit it by setting proxy = reset , and then, since counter now reset (no more than> = 600), all subsequent queues in the queue call SelectNewProxy() ? Feel that I am incoherent, but hopefully someone can understand what I'm trying to say, and if I am right in my assumptions, how can I guarantee that all streams will get proxy = reset and repeat our inital IP?

Any help is much appreciated!

Thankyou

+4
source share
2 answers

How did you announce the proxy ? If you read / write your value in several threads, you must make sure to declare it with the volatile keyword, otherwise the proxy entries in one thread may not be observed by others.

eg:.

 volatile WebProxy proxy; 
+1
source

It seems like you have a problem. If I correctly interpreted your message, you create a thread for every call to the request method that you posted.

Everything declared in a method is obviously part of your new thread. Anything outside the method will most likely not be part of your new thread. This means that you may encounter multiple threads trying to make stuff to indicate what is shared between the threads. In this case, you get access to the proxy from multiple threads. Since this is an instance of an object, you must block it before setting the proxy variable. Us lock (proxy) {// code using proxy} to make the thread safe for this variable.

0
source

All Articles