Threadpool help [modified]
-
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
-
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
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!
-
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!
-
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
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!