My problem I had was to do with the WebClient using the same thread as the UI, regardless of it being Async or not. I decided to use a BackgroundWorker instead.
Up vote 2 down vote favorite share g+ share fb share tw.
I have created an Asynchronous WebClient request inside a class as follows: public class Downstream { public bool StartDownstream() { WebClient client = new WebClient(); client.Headers. Add("user-agent", "Mozilla/4.0 ..."); client.Headers. Add("Content-Type","application/x-www-form-urlencoded"); try { byte postArray = Encoding.
UTF8. GetBytes("somevar=foo&someothervar=bar"); Uri uri = new Uri("examplesite.com/somepage.php"); client. UploadDataCompleted += new UploadDataCompletedEventHandler(client_UploadDataCompleted); client.
UploadDataAsync(uri, postArray); } catch (WebException e) { MessageBox. Show("A regular Web Exception"); } catch (NotSupportedException ne) { MessageBox. Show("A super Web Exception"); } return true; } void client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e) { MessageBox.
Show("The WebClient request completed"); } } I then create a new instance of the class and run the method here: Downstream Downstream1 = new Downstream(); Downstream1.StartDownstream(); When I do so, the thread that the form is running on seems to hang until the WebClient gets a response back. Why is this? I have used the UploadDataAsync method so should it not be Asynchronous?
Edit: This is my call stack: External Code > Arcturus. Exe!Arcturus.Downstream.StartDownstream() Line 36 + 0x18 bytes C# Arcturus. Exe!Arcturus.MainWindow.
BtnLogin_Click(object sender, System.Windows. RoutedEventArgs e) Line 111 + 0x12 bytes C# External Code This is all that happens when I run my application, just hanging on the StartDownstream() and client. UploadDataAsync(uri, postArray); methods.
Cheers c# multithreading asynchronous webclient link|improve this question edited Feb 7 '11 at 22:09bzlm4,26122042 asked Feb 7 '11 at 20:44Joel Kennedy8611415 86% accept rate.
– Mike Atlas Feb 7 '11 at 20:58 I have tested it and unfortunately this is the code that is blocking the calling thread. – Joel Kennedy Feb 7 '11 at 21:17 2 Use Debug + Break All while it blocks and copy the call stack. Do this a couple of times, post the one that repeats best.
– Hans Passant Feb 7 '11 at 21:31 I have posted my call stack above. Is that what you wanted to see? It doesn't seem like enough information!
– Joel Kennedy Feb 7 '11 at 21:57.
My problem I had was to do with the WebClient using the same thread as the UI, regardless of it being Async or not. I decided to use a BackgroundWorker instead. Thanks for the answers!
If you application doesn't have any COM calls then you can safely remove this attribute. Perhaps it may force UploadDataAsync to run in the same thread as the main form, thus blocking it.
I'm afraid as far as I can see my application doesn't have the STAThread attribute set. – Joel Kennedy Feb 7 '11 at 21:58.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.