Results 1 to 17 of 17

Thread: Non-Responsive App when Loading?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Non-Responsive App when Loading?

    Pretty much, as VB does, when a thread/function is in the process of being operated, the program becomes unresponsive (you cant do anything till that function is done executing)

    This is a problem for me...

    What I am trying to do is: When the form loads, it will launch a timer, on the timers event, what it should be doing is, displaying in a label what item it is currently in the process of unzipping, then after it is done, check that item off in the checkedlistbox below.

    What is happening is: When the form loads, it ill launch the timer, and on the timers event, it will display the current item being unzipped, and when that item is done, check it off on the checklistbox below, however you CANNOT see any of this happen, until it is done executing the code, it is only done executing the code once the last item is finished unzipping, then you will instantly see all the items go from unchecked to checked, and it say "Finished extracting"... Which defeats the entire purpose.

    So, what I want to know is: How do I make it so that you can actually SEE whats going on as it is happening, and the user can still interact (scroll down the list of items) with the program, while it is unzipping, and the user can actually see what item its currently on, and what items are done (indicated by it being checked or not)?

    I have all this code on a timer right now(with a 1 interval (1 millisecond)):

    Code:
        For Each file In archive.Entries
            label1.Text = "Currently Unzipping: " & file.FilePath & "..."
            If file.IsComplete = True Then
                'Check off each item once it is actually finished extracting to its destination...
                CheckedListBox.SetItemChecked(CheckedListBox.Items.IndexOf(file.FilePath), True)
            End If
        Next
    (As a side note: Im using SharpCompress.dll to actually find the archive, and do the appropriate action -- I already tried the SharpCompress forums, but I think this ones more general, more then related to the dll itself.)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Non-Responsive App when Loading?

    use a backgroundworker (secondary thread):

    http://msdn.microsoft.com/en-us/libr...(v=vs.95).aspx

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Non-Responsive App when Loading?

    Or, if your having difficulty with backgroundworker, a Thread object will do.

    Safe multithreading in winforms

    KG
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Non-Responsive App when Loading?

    The problem is that your foreground process is blocking ALL events from occuring, most notably the one that paints the screen when something changes. It seems likely that it is also blocking the timer tick events from happening, too, so I question whether or not the code you show is even being executed. It shouldn't be. If it was, then the screen would almost certainly be redrawing, too, which it is not.

    The two suggestions would solve this by shifting the time-consuming process off of the UI thread so that the UI thread is free to pump messages, paint the screen, and so forth.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: Non-Responsive App when Loading?

    Thanks for all the help. The brackground worker did in fact work just as I needed it to, also following this tutorial:
    http://www.youtube.com/watch?v=jv4FdoaUMQE

    Again, thanks for the help.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by Dibbie View Post
    Thanks for all the help. The brackground worker did in fact work just as I needed it to, also following this tutorial:
    http://www.youtube.com/watch?v=jv4FdoaUMQE

    Again, thanks for the help.
    Thats why you should stay away from youtube videos. That example updates a label under dowork which you should never do. Even more amusing he is using the reportprogress event. And finally programs with strict off. Oh actually later on "he realizes" the sillyness and edits it.

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Non-Responsive App when Loading?

    Oh my god, i knew this was coming. "checkforillegalcrossthreads set to false " :/ This is just stupid. The idiot person doing this clearly has no idea what teh BGW is since reportprogress actually handles the invoking for you :/

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by ident View Post
    Thats why you should stay away from youtube videos. That example updates a label under dowork which you should never do. Even more amusing he is using the reportprogress event. And finally programs with strict off. Oh actually later on "he realizes" the sillyness and edits it.
    I dont understand, it seemed to work exactly how I wanted it, and theres no lag issues, the only problem I see with his method is that it may later on use up some extra unwanted CPU, but by then, the unzipping process would have been completed long ago, and that data would (I assume) be dumped?

    What exactly is wrong with his method? And how might an alternative go?

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by Dibbie View Post
    I dont understand, it seemed to work exactly how I wanted it, and theres no lag issues, the only problem I see with his method is that it may later on use up some extra unwanted CPU, but by then, the unzipping process would have been completed long ago, and that data would (I assume) be dumped?

    What exactly is wrong with his method? And how might an alternative go?

    thats because you have no idea whats really in front of you. do not ever look at that link again. Checkforillegalcrossthreads is a massive no no.

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Non-Responsive App when Loading?

    do you want to learn proper?

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by ident View Post
    do you want to learn proper or ****?
    Well ofcourse I want to learn properly, and really learn coding (which is why I also appreciate Shaggy's response diagnosing the issue), and that is also why I asked why that method is bad.
    I understand blocking errors is obviously never good, but I dont understand why (aside from that) the method itself is bad?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by Dibbie View Post
    Well ofcourse I want to learn properly, and really learn coding (which is why I also appreciate Shaggy's response diagnosing the issue), and that is also why I asked why that method is bad.
    I understand blocking errors is obviously never good, but I dont understand why (aside from that) the method itself is bad?
    The best source of examples is MSDN, but even they're not perfect. YouTube examples are notoriously hackish, with most posted by beginners or misinformed programmers

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Non-Responsive App when Loading?

    Quote Originally Posted by Dibbie View Post
    Well ofcourse I want to learn properly, and really learn coding (which is why I also appreciate Shaggy's response diagnosing the issue), and that is also why I asked why that method is bad.
    I understand blocking errors is obviously never good, but I dont understand why (aside from that) the method itself is bad?
    Check out Johns article http://www.vbforums.com/showthread.p...rker-Component

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Non-Responsive App when Loading?

    The reason is that you can have two threads accessing the same object at the same time and that can corrupt the state of the object. The earlier versions of Visual Studio (before 2005) allowed the background thread to access the GUI controls and that lead to spurious, i.e. intermittent, problems with the application. The idea was that if you were going to do cross thread access to objects you would implement locking mechanisms to prevent simultaneous access, but since creating a background thread was so easy with .Net, you had many people accessing controls from background threads with no protection mechanisms in place, and the result was occasional random errors in the application.
    So, starting with VS 2005, accessing the GUI controls from a background thread was made "illegal", which should push the user to use provided Invoke methods to invoke a routine on the GUI thread so that controls are only updated from the GUI thread, thereby avoiding the simultaneous thread access issue.
    But, for the knowlegeable person who understands the ramifications of using multiple threads, and who properly implements mechanism to prevent multiple simulataneous access to the controls, Checkforillegalcrossthreads is provided to override the check and allow the user to write code to access the controls from a background thread.
    Unfortunately, that link is not an example of properly accounting for multi-thread access, so can lead to spurious errors, even though it may appear to work fine the majority of the time.
    It would be best to just use the built-in features that provide a link between the threads through invocation to update the controls, and avoid overriding the "illegal" access.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Non-Responsive App when Loading?

    CheckForIllegalCrossThreads is mostly bad because it can lead to race conditions, which are the most annoying of all possible bugs to deal with. If you have a race condition, the code may work forever on one computer, never on another, or work for days and then suddenly, without warning, never work again. A race condition just means that two threads are doing something that interacts in some way and the actual result depends on who gets to that thing first. If thread A or thread B ALWAYS gets to the thing first, then the code will always behave the same way. However, it's a race, and the winner depends on the conditions of the race. Different computers can have different processors, different operation loads, and other factors, which can impact who reaches the critical section first. The behavior of the program becomes inherently unpredictable under those conditions, but it can be different on different hardware, with different software installed, or on different days of the week. As you might imagine, if a program doesn't do the same thing twice in a row under what appears to be the same initial conditions, it gets very hard to figure out the cause.

    Accessing controls from other threads can cause race conditions to arise. Many times, you can get away with turning CheckForIllegalCrossThreads to False because either no race conditions arise in your particular case, or else there is always the same winner due to some quirk of chance. Of course, that doesn't mean that it will be safe the very next time you run the same program, let alone use the same technique in a different program. In other words, you are rolling the dice and if you lose....you'll have a helluva time figuring out why.
    My usual boring signature: Nothing

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Non-Responsive App when Loading?

    I really didn't think I'd cross up somebody else with that explanation.
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: Non-Responsive App when Loading?

    Thanks for all the insight guys.

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