Results 1 to 14 of 14

Thread: Can someone explain Why this doesn't work all the time.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Can someone explain Why this doesn't work all the time.

    I am trying to download a file in my application.. If I call it from a button or menu click event it works but in other callings it does not and returns a 0 length file and never attempts to download the file..

    Code:
     Private Sub ClickMenu_Click(sender As Object, e As EventArgs) Handles ClickMenu.Click
            Dim client As New WebClient()
            AddHandler client.DownloadProgressChanged, AddressOf ShowDownloadProgress
            AddHandler client.DownloadFileCompleted, AddressOf OnDownloadComplete
            client.DownloadFileAsync(New Uri("http://www.n2amg.com/files/L32LookupsUpdate.zip"), AppPath & "\Update\L32LookupsUpdate.zip")
        End Sub
    This above works perfect and 100% of the time it downloads the file correctly and shows progress and completion status.

    Code:
     Private Sub Download()
            Dim client As New WebClient()
            AddHandler client.DownloadProgressChanged, AddressOf ShowDownloadProgress
            AddHandler client.DownloadFileCompleted, AddressOf OnDownloadComplete
            client.DownloadFileAsync(New Uri("http://www.n2amg.com/files/L32LookupsUpdate.zip"), AppPath & "\Update\L32LookupsUpdate.zip")
        End Sub
    This above does not work. It returns a zero length file and never attempts to download the file as soon as it starts it exits the routine and there is no progress or completion status and no errors.

    Code:
    ClickMenu_Click(ClickMenu, New System.EventArgs)
    Calling the working routine like above does not work either the routine returns the same zero length file and actions as above..

    If I put the routine in a form by itself and call it in the load routine it works correctly..

    I'm puzzled..

    TIA
    Rick

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can someone explain Why this doesn't work all the time.

    1) where is the Download routine located? This suggests that it is not in the form "If I put the routine in a form by itself " -- so where is it?
    2) where are you calling Download from?
    3) Where are the subs for ShowDownloadProgress and OnDownloadcomplete located? In the form or where Download is located?
    4) Does the progress update routine attempt to use the form for anything?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: Can someone explain Why this doesn't work all the time.

    Aside from that, I'm not entirely convinced that the menu option really is working as well as you think it is. Every time you click that, you create a WebClient and hook up a couple handlers. Unless you are also releasing those handlers, which doesn't seem likely, that WebClient will hang around for the life of the project. That may not matter much in this case, but if the program runs for a long time with many clicks on that menu, the memory will grow and grow as more and more WebClient objects get created and live on forever.
    My usual boring signature: Nothing

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Can someone explain Why this doesn't work all the time.

    Quote Originally Posted by Shaggy Hiker View Post
    Aside from that, I'm not entirely convinced that the menu option really is working as well as you think it is. Every time you click that, you create a WebClient and hook up a couple handlers. Unless you are also releasing those handlers, which doesn't seem likely, that WebClient will hang around for the life of the project. That may not matter much in this case, but if the program runs for a long time with many clicks on that menu, the memory will grow and grow as more and more WebClient objects get created and live on forever.
    In addition to that, WebClients implement IDisposable, which means they should have .dispose called on them when they are done being used to free up the unmanaged resources.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: Can someone explain Why this doesn't work all the time.

    1. & 2. All of these routines are located in a Form. Its the main form of my application. the routine that I was trying to use is this:
    Code:
      Private Sub DownloadUpdates()
            StatusLabel.Text = "Downloading Update..."
            Download
            UnzipUpdate()
            File.Copy(iniFileName, AppPath & "\Update\L32Lookups.ini", True)
            LoadUpdater()
            Me.Close()
        End Sub
    What I meant by the other form is when I could not get the Download routine to work correctly I added a new form to my project for testing. In the load event I placed the above routine and added in the download routine. As soon as the form opens it would download the file and work as it should. But I do not want to use a separate form for the download.

    3. The subs are located directly below the above routine in the same form.

    4. Yes it updates a progressbar located in the bottom statusbar.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can someone explain Why this doesn't work all the time.

    And where is DownloadUpdates called from? Have you tried setting breakpoints and stepping through the code to see where it's going and when?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: Can someone explain Why this doesn't work all the time.

    The menu option was just a test to see if the download routine would work if I placed it in it's click event. and it did . As my post above I was trying to run this from a download routine and the main form would have been closed right after the download was unzipped and the update program loaded. If I have to use a button click event then I then I still would not worry about the running of the routine over and over as it would only be called if there was an update available. and right after the download takes place the form would be closed so the update program can do its work.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: Can someone explain Why this doesn't work all the time.

    See above post

    (Breakpoints) Yes I have
    as soon as the downloadfileasync statement is called a 0 length file is placed in the directory it is supposed to be in and the the End Sub breakpoint is hit. no error messages or anything from the webclient is returned.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can someone explain Why this doesn't work all the time.

    so is the call in the Form Load event or what? if all it is doing is downloading something and then moving on, then maybe a winform isn't the way to go... or you need something else... hard to say because I keep asking where it's caleld from and I get a dissertation on what the app is supposed to do... great, I got that... ok, so where is DownloadUpdates being called from?! If it's not being called from anywhere, then of course it's never going to run... it's not going to magically know, Oh, OK, when I load, I need to run this... unless you tell it to. If it's working behind the click, then the code works... so when you remove it from the click event, where are you calling it from?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can someone explain Why this doesn't work all the time.

    Are you calling the code in the form Load event by chance... and are you running this on a Win7 64-bit machine perchance?
    If so, there might be a reason it "doesn't work"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: Can someone explain Why this doesn't work all the time.

    There might be a different reason, too. At this point, with the various descriptions, I no longer have a clear view of what is working and what is not. If the code doesn't run from the main form, but does run from a new form created from the main form, then there could be a different issue going on. The LoadEvent issue would be the quiet swallowing of exceptions thrown in that event. However, if this is run on the main form, that would be a default instance of a form. Who gets the events raised in that case?
    My usual boring signature: Nothing

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Can someone explain Why this doesn't work all the time.

    I'd like to see way more code, particularly OnDownloadComplete(), but not limited to it. That's where you'd be notified if an error occured. It could be that you're getting far enough for the MS code to want to create the file, or that it does that as a first step, but something fails before it finishes. I need to see what calls Download(), what calls that, etc. to properly know why it's different than OnLoad().

    If you feel like you can't post the entirety of your project, create a new project that ONLY has the code to download this one file and upload that code after verifying it doesn't work. Sometimes, when you do this, you find that the small, one-purpose project works when the larger project doesn't. This means you've identified that there's something different in your larger project. But we can't really help you when we can't see all of the code involved. There's too many different things that could be going wrong.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Can someone explain Why this doesn't work all the time.

    Quote Originally Posted by rellison View Post
    ... the routine that I was trying to use is this:
    Code:
      Private Sub DownloadUpdates()
            StatusLabel.Text = "Downloading Update..."
            Download
            UnzipUpdate()
            File.Copy(iniFileName, AppPath & "\Update\L32Lookups.ini", True)
            LoadUpdater()
            Me.Close()
        End Sub
    ... In the load event I placed the above routine and added in the download routine. As soon as the form opens it would download the file and work as it should.
    Are you sure about that? It probably downloads the file, but I'd expect an error to occur in your UnzipUpdate method.

    The above method calls your DownLoad method, which makes a call to WebClient.DownLoadAsync. That method is not a blocking call. It starts the download off, and then returns immediately. So a fraction of a second after the download starts, you are calling your UnzipUpdate method. The download will be no where near complete, and so I'd expect an error as you try to unzip an empty file.


    Most probably that error is being swallowed for the reason outlined by tg in Post#10.

    That would mean the rest of the code in your DownloadUpdates method would not be executed. The file would not be unpacked, the data would not be copied to a new location, and the Form would not close. However, the file would continue to download and update the progress bar.


    You should process the file only after it has completed downloading, in the DownloadFileCompleted Event handler.

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: Can someone explain Why this doesn't work all the time.

    BINGO!!!!

    "The above method calls your DownLoad method, which makes a call to WebClient.DownLoadAsync. That method is not a blocking call. It starts the download off, and then returns immediately. So a fraction of a second after the download starts, you are calling your UnzipUpdate method. The download will be no where near complete, and so I'd expect an error as you try to unzip an empty file."

    Esp this part:That method is not a blocking call.

    When I removed the other routine calls and placed them in the completed event then everything started to work the way it should...
    For some reason I was thinking that this blocked things until it finished (Not really sure where I got that from..) But once I did wait then it started working like it should!

    Thanks for opening my eyes to that I just was not grasping that part..

    Rick

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