I have a for loop that I want (for every item in a ListBox) to execute a method.
What happening now is the first item is being selected, the method is being executed, but then it doesn’t select the second item, it just sits there.
Can you help?
This is how my for loop looks:
for(int i = 0; i < listBox8.Items.Count; i++) {
listBox8.SetSelected(i, true);
listBox8.SelectedIndex = 0;
Thread t = new Thread(signinmobile);
t.Start();
CheckForIllegalCrossThreadCalls = false;
}
And here is my sub:
public void signinmobile()
{
string yourString = listBox8.SelectedItem.ToString();
string[] strArray = yourString.Split(':');
System.Net.ServicePointManager.Expect100Continue = false;
string postData = "authenticity_token=401538d41ace8f334c3d&username=" + strArray[0] + "&password=" + strArray[1] + "";
CookieContainer tempCookies = new CookieContainer();
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(postData);
HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("https://mobile.twitter.com/session");
postReq.Method = "POST";
postReq.KeepAlive = true;
postReq.CookieContainer = tempCookies;
postReq.ContentType = "application/x-www-form-urlencoded";
postReq.Referer = "https://mobile.twitter.com/session";
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)";
postReq.ContentLength = byteData.Length;
Stream postreqstream = postReq.GetRequestStream();
postreqstream.Write(byteData, 0, byteData.Length);
postreqstream.Close();
HttpWebResponse postresponse = default(HttpWebResponse);
postresponse = (HttpWebResponse)postReq.GetResponse();
tempCookies.Add(postresponse.Cookies);
StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());
string accountstatus = postreqreader.ReadToEnd();
webBrowser1.DocumentText = accountstatus;
if (accountstatus.Contains("Sign in information is not correct"))
{
listBox9.Items.Add(listBox8.SelectedItem.ToString() + "\r");
while (listBox8.SelectedItems.Count > 0)
{
listBox8.Items.Remove(listBox8.SelectedItems[0]);
}
}
else
{
listBox2.Items.Add(listBox8.SelectedItem.ToString() + "\r");
while (listBox8.SelectedItems.Count > 0)
{
listBox8.Items.Remove(listBox8.SelectedItems[0]);
}
}
}
Following line always selecting the first item for each loop cycle
I believe you can remove this line at all because previous one (
listBox8.SetSelected(i, true);) already does selectionEDIT: Since question was updated
I feel that here is exception may occur. Add
try/catch(Exception ex)block aroundsigninmobilemethod call and say us whether eny exception was handled.BTW, Why you are running method in an other thread? Looks like there is thread sync issue so if list contains more then two items multiple threads would run and removing items in list, and then call to
SetSelectedfails since it cached index valueiwhich currently not exists since some thread already removed item… So run all in single thread or dot.Join()aftert.Start()so main thread would wait until working thread is done and them continue next loop cycle.