•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,472 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,778 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 346 | Replies: 2 | Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Here is my situation. Im trying to learn how to take full advantage of multithreading but am at a rump.
I have one listbox of usernames and it checks them on a website to see if they exist or not. If they exist it puts the username into listbox2.
Now that portion works fine. But im wondering how i can use say 5 threads to go through the list and check the names quicker. My code is as follows:
My problem is i wouldnt know how to tell it to skip the username that is being checked by another thread. Any help is greatly appreciated.
I have one listbox of usernames and it checks them on a website to see if they exist or not. If they exist it puts the username into listbox2.
Now that portion works fine. But im wondering how i can use say 5 threads to go through the list and check the names quicker. My code is as follows:
//Start
public void button5_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < 1; i++)
{
// listBox1.SelectedIndex = i;
Thread thread1 = new Thread(new ThreadStart(this.getSite));
thread1.Start();
while (thread1.IsAlive)
{
Application.DoEvents();
}
if(!(thread1.IsAlive))
{
label2.Text = "Count: " + listBox2.Items.Count;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public void getSite()
{
for (int j = 0; j < listBox1.Items.Count; j++)
{
string uri = @"http://www.website.com";
string username = listBox1.Items[j].ToString();
string data = "accountName=" + username;
string source = "";
source = getInfo(data, uri);
if (source.Contains("nswer"))
{
listBox2.Invoke(new UpdateTextCallback(this.addUsername), new object[] { username });
}
}
}// END GET SITE
//Used to invoke username into listbox2
public void addUsername(string user)
{
// Add the listbox text
listBox2.Items.Add(user);
}
//Used to invoke username into listbox2
public delegate void UpdateTextCallback(string user);
public static string getInfo(string DATA, string URL)
{
HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URL);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
// Encode data and post it
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(DATA);
req.ContentLength = DATA.Length;
Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
// Get response
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader ResponseStream = new StreamReader(resp.GetResponseStream());
string Source = "";
Source = ResponseStream.ReadToEnd();
return Source;
}My problem is i wouldnt know how to tell it to skip the username that is being checked by another thread. Any help is greatly appreciated.
•
•
Join Date: Aug 2008
Posts: 872
Reputation:
Rep Power: 3
Solved Threads: 79
Kil the whole application doevents bit, thats a sure way to tie yourself in knots.
Have a list items you've queued up to go, fire off 5, as each of the 5 finish, take the next one off the queue and start a new thread. until you have nothing left in the queue.
At the end of each threadrun, issue an event to say "A thread finished, heres the result" ..
Have a list items you've queued up to go, fire off 5, as each of the 5 finish, take the next one off the queue and start a new thread. until you have nothing left in the queue.
At the end of each threadrun, issue an event to say "A thread finished, heres the result" ..
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Cpu 100% !?! (Windows NT / 2000 / XP / 2003)
Other Threads in the C# Forum
- Previous Thread: Some quick help with loggin into a webpage
- Next Thread: Datatable


Linear Mode