Results 1 to 11 of 11

Thread: Form loading issue!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Angry Form loading issue!

    Today I have a problem. When I try to load a form (well a specific form, the others are fine), it has a massive bug where the controls will make a "hole" of transparency and then within about 10 seconds it will just automatically close. As well as that, the cursor switches to the Windows "loading" cursor. I don't know why it does this, I haven't added in any code except the following to make it show at the bottom right of the screen. To do this, I used the following code however I do not think it is related to this issue:

    Code:
    Dim x As Integer
        Dim y As Integer
        x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width - 5
        y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
        Me.Location = New Point(x, y)
        Me.BringToFront()
    When I set the loading entry form in Visual Studio project settings, it loads perfect without any issues, however whenever I try to show it from code in my application like:

    Code:
    Dim frm1 As New updateNotify 
    frm1.Show()
    It seems not to work and have this bug. It does the same when I just call it as:

    Code:
    updateNotify.Show()
    Last edited by visualBEER310; Sep 20th, 2014 at 08:38 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    EDIT: I noticed it happens to every form, however only when it is called in a specific event which is handled. The form is shown in the DownloadFileCompleted event of a webclient, and that's the only place I get this bug when showing it. When I make it appear anywhere else, it works fine. I've also tried making it call a method but if it's linked to being shown with this event at all it gives me the bug. Any ideas? It's strange...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Form loading issue!

    Try this:
    Code:
    Dim frm1 As New updateNotify 
    frm1.ShowDialog()
    See if that works.

    I suspect the issue is one of scope. By using Show, the code continues to run, and the variable falls out of scope

    -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??? *

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    Quote Originally Posted by techgnome View Post
    Try this:
    Code:
    Dim frm1 As New updateNotify 
    frm1.ShowDialog()
    See if that works.

    I suspect the issue is one of scope. By using Show, the code continues to run, and the variable falls out of scope

    -tg
    Your a legend! That was the only way that made it work!

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Form loading issue!

    Quote Originally Posted by visualBEER310 View Post
    EDIT: I noticed it happens to every form, however only when it is called in a specific event which is handled. The form is shown in the DownloadFileCompleted event of a webclient, and that's the only place I get this bug when showing it. When I make it appear anywhere else, it works fine. I've also tried making it call a method but if it's linked to being shown with this event at all it gives me the bug. Any ideas? It's strange...
    Quote Originally Posted by visualBEER310 View Post
    .... That was the only way that made it work!
    That may work, but I suspect it is just a hack that allows it to do so.

    Most likely you have not synchronized the code execution to the UI thread and are creating the new form on a thread different than the primary UI thread.

    I may have this following explanation a bit wrong, but the ShowDialog method creates a new message loop for the form to run in thus allowing this to look as though it is working. Most likely you are able to interact with the form that executes the ShowDialog method and this is not normal behavior.

    Oh, well now to try and give you something to correct the issue. Instead of using:
    Code:
    WebClient.DownloadFileAsync(Uri, String)
    to start the download, use the overload that allows you to pass a synchronization object
    Code:
    WebClient.DownloadFileAsync(Uri, String, Object)
    . This synchronization object can be the current form instance represented by the "Me" keyword.
    Code:
    WebClient.DownloadFileAsync(new uri("path to download"), "save path", Me)
    Last edited by TnTinMN; Sep 20th, 2014 at 09:24 AM. Reason: typo

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    Quote Originally Posted by TnTinMN View Post
    That may work, but I suspect it is just a hack that allows it to do so.

    Most likely you have not synchronized the code execution to the UI thread and are creating the new form on a thread different than the primary UI thread.

    I may have this following explanation a bit wrong, but the ShowDialog method creates a new message loop for the form to run in thus allowing this to look as though it is working. Most likely you are able to interact with the form that executes the ShowDialog method and this is not normal behavior.

    Oh, well now to try and give you something to correct the issue. Instead of using:
    Code:
    WebClient.DownloadFileAsync(Uri, String)
    to start the download, use the overload that allows you to pass a synchronization object
    Code:
    WebClient.DownloadFileAsync(Uri, String, Object)
    . This synchronization object can be the current form instance represented by the "Me" keyword.
    Code:
    WebClient.DownloadFileAsync(new uri("path to download"), "save path", Me)
    Yes, you were right. I forgot to mention that I had been using threads on the method which was using the Webclient to download a file. Thank you, I will try your suggestions. The first one works though, but I will also try yours.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    Quote Originally Posted by visualBEER310 View Post
    Yes, you were right. I forgot to mention that I had been using threads on the method which was using the Webclient to download a file. Thank you, I will try your suggestions. The first one works though, but I will also try yours.
    Still gave me errors, so I shall use the first suggestion by the first poster here

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Form loading issue!

    I forgot to mention that I had been using threads on the method
    Oh sweet ... yeah, that was kind of an important thing there...
    TnTinMN is right, it is something of a hack in this case... it works because the SHowDialog is a blocking call... the thread it's on can't continue until it closes. Since that is being done on a secondary thread, that's actually a bad place for it. It really should be handled when the thread i done and comes back to the main UI Thread.

    How are you creating and starting these threads?

    -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??? *

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Form loading issue!

    Quote Originally Posted by visualBEER310 View Post
    Yes, you were right. I forgot to mention that I had been using threads on the method which was using the Webclient to download a file. Thank you, I will try your suggestions. The first one works though, but I will also try yours.
    Quote Originally Posted by visualBEER310 View Post
    Still gave me errors, so I shall use the first suggestion by the first poster here
    Darn, I will have to look into why the sync object does not work later. Time to get a bigger hammer to squash this bug.

    Let's make sure that it runs on the UI thread regardless of where the event is fired from. Modify your eventhandler code to be something like this.

    Code:
    Private Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
       If Me.InvokeRequired Then
          Me.Invoke(New Action(AddressOf ShowupdateNotify))
       Else
          ShowupdateNotify()
       End If
    End Sub
    Private Sub ShowupdateNotify()
       Dim f As New updateNotify
       f.Show()
    End Sub
    Last edited by TnTinMN; Sep 20th, 2014 at 08:23 PM. Reason: typo

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    Quote Originally Posted by TnTinMN View Post
    Darn, I will have to look into why the sync object does not work later. Time to get a bigger hammer to squash this bug.

    Let's make sure that it runs on the UI thread regardless of where the event is fired from. Modify your eventhandler code to be something like this.

    Code:
    Private Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
       If Me.InvokeRequired Then
          Me.Invoke(New Action(AddressOf ShowupdateNotify))
       Else
          ShowupdateNotify()
       End If
    End Sub
    Private Sub ShowupdateNotify()
       Dim f As New updateNotify
       f.Show()
    End Sub
    Okay, I shall try that now

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Form loading issue!

    Quote Originally Posted by TnTinMN View Post
    Darn, I will have to look into why the sync object does not work later. Time to get a bigger hammer to squash this bug.

    Let's make sure that it runs on the UI thread regardless of where the event is fired from. Modify your eventhandler code to be something like this.

    Code:
    Private Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
       If Me.InvokeRequired Then
          Me.Invoke(New Action(AddressOf ShowupdateNotify))
       Else
          ShowupdateNotify()
       End If
    End Sub
    Private Sub ShowupdateNotify()
       Dim f As New updateNotify
       f.Show()
    End Sub
    Yes, it's working great!!
    Thank you!

Tags for this 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
  •  



Click Here to Expand Forum to Full Width