Thursday, 22 August 2013

Why is this hanging?

Why is this hanging?

private async void button1_Click(object sender, EventArgs e)
{
var cookie = webBrowser1.Document.Cookie;
await
Task.WhenAll(
listBox1
.Items
.Cast<string>()
.Select(async s =>
{
var data = "action=relationship&user_id=" + s +
"&relation=follow";
var req = WebRequest.Create("http://example.com") as
HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
req.Headers["cookie"] = cookie;
using (var sw = new StreamWriter(await
req.GetRequestStreamAsync(), Encoding.ASCII))
{
sw.Write(data);
sw.Close();
}
}));
listBox1.Items.Clear();
}
I've tried this a million times, and it runs it two times every time. And
when it runs it those two times, it does what it's supposed to.
What it's supposed to do is take items from a listbox, and use each item
as part of a POST request, then when it's done, clear the listbox.
Can someone explain what's wrong?
Thank you.

No comments:

Post a Comment