|
-
Sep 20th, 2014, 06:24 AM
#1
Thread Starter
Lively Member
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.
-
Sep 20th, 2014, 07:06 AM
#2
Thread Starter
Lively Member
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...
-
Sep 20th, 2014, 07:44 AM
#3
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
-
Sep 20th, 2014, 08:37 AM
#4
Thread Starter
Lively Member
Re: Form loading issue!
 Originally Posted by techgnome
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!
-
Sep 20th, 2014, 09:22 AM
#5
Re: Form loading issue!
 Originally Posted by visualBEER310
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...
 Originally Posted by visualBEER310
.... 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
-
Sep 20th, 2014, 09:30 AM
#6
Thread Starter
Lively Member
Re: Form loading issue!
 Originally Posted by TnTinMN
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.
-
Sep 20th, 2014, 09:32 AM
#7
Thread Starter
Lively Member
Re: Form loading issue!
 Originally Posted by visualBEER310
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
-
Sep 20th, 2014, 09:42 AM
#8
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
-
Sep 20th, 2014, 10:47 AM
#9
Re: Form loading issue!
 Originally Posted by visualBEER310
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.
 Originally Posted by visualBEER310
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
-
Sep 21st, 2014, 04:17 AM
#10
Thread Starter
Lively Member
Re: Form loading issue!
 Originally Posted by TnTinMN
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
-
Sep 21st, 2014, 04:22 AM
#11
Thread Starter
Lively Member
Re: Form loading issue!
 Originally Posted by TnTinMN
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|