Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threadpool help [modified]

Threadpool help [modified]

Scheduled Pinned Locked Moved C#
csharpcomgraphicshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OptiPlex
    wrote on last edited by
    #1

    Hello, Im trying to make a multithreaded proxy tester/scanner. http://i36.tinypic.com/2m5jllf.jpg[^] here is my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using System.Threading;

    namespace WebProxyTest
    {
    public partial class Main : Form
    {
    public Main()
    {
    InitializeComponent();
    }

        private void Main\_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }
    
        private void btnStart\_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < lstProxies.Items.Count; i++){
                string proxy = lstProxies.Items\[i\].ToString();
    
                string\[\] proxyinfo = proxy.Split(':');
                string proxyhost = proxyinfo\[0\];
                int proxyport = Convert.ToInt32(proxyinfo\[1\]);
                WebProxyTest test = new WebProxyTest(proxyhost, proxyport, "http://google.com/");
    
                ThreadPool.SetMaxThreads(10, 2);
                ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessProxies), test);
            }
        }
    
        private void ProcessProxies(object state){
            WebProxyTest test = state as WebProxyTest;
            test.TestProxy();
            //lstProxies.Items.Add("Result : " + test.ProxyResult);
            lstResults.Items.Add("Result: " + test.ProxyResult);
        }
    
    }
    

    }

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.IO;
    using System.Web;
    using System.Net;

    namespace WebProxyTest
    {
    class WebProxyTest
    {
    private string proxyhost;
    private int proxyport;
    private string url;

        private string proxy\_result = "none";
        public string ProxyResult {
            get {
                return proxy\_result;
            }
            set {
                proxy\_result = value;
            }
        }
    
        public WebProxyTest(string proxyhost, int proxyport, string url)
        {
            this.proxyhost = proxyhost;
            this.proxyport = proxyport;
            this.url = url;
        }
    
        public void TestProxy()
        {
            try
            {
    
                WebProxy proxy = new WebProxy
    
    L 1 Reply Last reply
    0
    • O OptiPlex

      Hello, Im trying to make a multithreaded proxy tester/scanner. http://i36.tinypic.com/2m5jllf.jpg[^] here is my code:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;

      using System.Threading;

      namespace WebProxyTest
      {
      public partial class Main : Form
      {
      public Main()
      {
      InitializeComponent();
      }

          private void Main\_Load(object sender, EventArgs e)
          {
              CheckForIllegalCrossThreadCalls = false;
          }
      
          private void btnStart\_Click(object sender, EventArgs e)
          {
              for(int i = 0; i < lstProxies.Items.Count; i++){
                  string proxy = lstProxies.Items\[i\].ToString();
      
                  string\[\] proxyinfo = proxy.Split(':');
                  string proxyhost = proxyinfo\[0\];
                  int proxyport = Convert.ToInt32(proxyinfo\[1\]);
                  WebProxyTest test = new WebProxyTest(proxyhost, proxyport, "http://google.com/");
      
                  ThreadPool.SetMaxThreads(10, 2);
                  ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessProxies), test);
              }
          }
      
          private void ProcessProxies(object state){
              WebProxyTest test = state as WebProxyTest;
              test.TestProxy();
              //lstProxies.Items.Add("Result : " + test.ProxyResult);
              lstResults.Items.Add("Result: " + test.ProxyResult);
          }
      
      }
      

      }

      using System;
      using System.Collections.Generic;
      using System.Text;

      using System.IO;
      using System.Web;
      using System.Net;

      namespace WebProxyTest
      {
      class WebProxyTest
      {
      private string proxyhost;
      private int proxyport;
      private string url;

          private string proxy\_result = "none";
          public string ProxyResult {
              get {
                  return proxy\_result;
              }
              set {
                  proxy\_result = value;
              }
          }
      
          public WebProxyTest(string proxyhost, int proxyport, string url)
          {
              this.proxyhost = proxyhost;
              this.proxyport = proxyport;
              this.url = url;
          }
      
          public void TestProxy()
          {
              try
              {
      
                  WebProxy proxy = new WebProxy
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, there could be two issues: 1.

      OptiPlex wrote:

      CheckForIllegalCrossThreadCalls = false;

      IMO is a very bad idea. If you feel a need to disable those checks, something is fundamentally wrong. Please read this article[^]. 2. AFAIK a lot of (all?) COM components (and that includes the Clipboard) require to be handled only by threads that are in a "Single Threaded Apartment", which also is a way to obtain thread safety. You can get things right by either specifying [STAThread] in front of your public static Main() method (which is there by default IIRC), or by setting it explicitly on the relevant thread, using Thread.GetCurrentThread().SetThreadAppartmentState(). [EDIT] As one can set the AppState only once for a thread, this may not work for ThreadPool threads; so you may have to create your own Thread(s) instead. [/EDIT] :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      O 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, there could be two issues: 1.

        OptiPlex wrote:

        CheckForIllegalCrossThreadCalls = false;

        IMO is a very bad idea. If you feel a need to disable those checks, something is fundamentally wrong. Please read this article[^]. 2. AFAIK a lot of (all?) COM components (and that includes the Clipboard) require to be handled only by threads that are in a "Single Threaded Apartment", which also is a way to obtain thread safety. You can get things right by either specifying [STAThread] in front of your public static Main() method (which is there by default IIRC), or by setting it explicitly on the relevant thread, using Thread.GetCurrentThread().SetThreadAppartmentState(). [EDIT] As one can set the AppState only once for a thread, this may not work for ThreadPool threads; so you may have to create your own Thread(s) instead. [/EDIT] :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        O Offline
        O Offline
        OptiPlex
        wrote on last edited by
        #3

        thanks allot for your reply. I was checking out the link u posted, but it seems like thats for controls/ui operations only. Now im still looking for other sollutions :sigh: thanks again

        L 1 Reply Last reply
        0
        • O OptiPlex

          thanks allot for your reply. I was checking out the link u posted, but it seems like thats for controls/ui operations only. Now im still looking for other sollutions :sigh: thanks again

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          As I said earlier, using CheckForIllegalCrossThreadCalls is bad, whatever your app is, as it does not solve anything, it is completely useless (that is what the article is about). So you should fix that; and then do something about STA as I indicated. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups