|
-
May 19th, 2008, 03:14 AM
#1
Thread Starter
Addicted Member
Not responding! How to sovle?
I here have some codes that make my app become "Non responding" when I click the mouse on its forms. I want my app to work in the background, and let the user see the progress bar, and the progress title showing the status of the application. How can I do this? And where articles about this matter?
Anyone here please help me.
The code make the app not responding below:
Code:
WebClient wc = new WebClient();
string s;
try
{
Stream data = wc.OpenRead(url);
StreamReader sd = new StreamReader(data);
s = sd.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("Chương trình gặp lỗi: " + ex.Message, "Lỗi chương trình", MessageBoxButtons.OK, MessageBoxIcon.Error);
s = "";
}
Thanks for reading!
-
May 19th, 2008, 04:12 AM
#2
Re: Not responding! How to sovle?
You need to run the code in a separate thread. Use the BackgroundWorker component. Read about it on MSDN, or have a look at the two links in jmcilhinney's signature.
Altough I suppose you could also change that entire block of code to a call to one of the webclients asynchronous download methods.
-
May 19th, 2008, 04:16 AM
#3
Thread Starter
Addicted Member
Re: Not responding! How to sovle?
I am new with this C, so I may understand only 1/2 of your words above. Where can I find Jimcilhinney to get his Signature? and why do i have to cange the block of code while Webclient already have the download method?
-
May 19th, 2008, 05:46 AM
#4
Re: Not responding! How to sovle?
Here I am, but you didn't need me to post here. There's a "Member List" link just above your first post. You can find any member that way.
As to why you have to change things, it's because your app currently runs in only one thread. If that thread is busy downloading the data for the WebClient then it can't be updating your UI too, hence your app won't respond while the download is in progress.
Given that the WebClient provides both a synchronous (which you're using) and an asynchronous (which uses a background thread) model, it would be preferable to use the asynchronous model rather than using a BackgroundWorker. You should call the DownloadStringAsync method of your WebClient. That will download the data to a String in a background thread, leaving the main thread free to update your UI and keep your app responsive. When the download is complete the DownloadStringCompleted event is raised and you can get your data. I suggest that you read the relevant MSDN documentation.
-
May 19th, 2008, 08:50 PM
#5
Thread Starter
Addicted Member
Re: Not responding! How to sovle?
I know these methods about download data in webclient class, but I don't use because of the purpose of my project.
In my project, I will get links from a webpage, and then use the webclient instance to browse all the links and display the result in some places. So if i use the downloadDataComplete method then I cannot browse the array of links above.
Maybe I going to read about thread.
-
May 19th, 2008, 10:19 PM
#6
Thread Starter
Addicted Member
Re: Not responding! How to sovle?
Is there any static method like Doevents in VB6?
-
May 19th, 2008, 10:50 PM
#7
Re: Not responding! How to sovle?
Application.DoEvents but it's considered a hack. You should use multithreading of some description. I still don't see the issue with using an asynchronous method.
-
May 21st, 2008, 12:27 PM
#8
Re: Not responding! How to sovle?
 Originally Posted by Bahy
Is there any static method like Doevents in VB6?
Wouldn't help you anyway. If you put it before the call the UI will update then wait for the download. if you put it after the call then your UI will update after the call.
The best solution is using another thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|