Results 1 to 8 of 8

Thread: Not responding! How to sovle?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Viet Nam
    Posts
    142

    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!

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Viet Nam
    Posts
    142

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Viet Nam
    Posts
    142

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Viet Nam
    Posts
    142

    Re: Not responding! How to sovle?

    Is there any static method like Doevents in VB6?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Not responding! How to sovle?

    Quote 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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width