Assembly load progress bar/Splash screen

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Assembly load progress bar/Splash screen [Resolved]

Hi, a little while ago there was a thread regarding splash screens that ended up in a bit of an argument i hope this one doesnt go that way....

What i would like to do, is that i have written a large app that takes some time to load. I would like to display a progress bar immediately to show the progress of the application loading to the user. Anyone got a good links or ideas.
 
Last edited:
You can use the main form load event, show a progress-form, input progress values/info depending on what you know is loading. There is a small sample here, I simulate loading steps with a thread.sleep.
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim formprogress As New Form2
  formprogress.progressmax = 2
  formprogress.progressvalue = 0
  formprogress.progresstext = "loading step 1"
  formprogress.Show()
  Application.DoEvents()
  Threading.Thread.Sleep(1000)
  formprogress.progressvalue += 1
  formprogress.progresstext = "loading step 2"
  Application.DoEvents()
  Threading.Thread.Sleep(1000)
  formprogress.progressvalue += 1
  formprogress.progresstext = "loading step 3"
  Application.DoEvents()
  Threading.Thread.Sleep(1000)
  formprogress.Hide()
  formprogress = Nothing 
End Sub
 
Just one point. That code makes the progress form not visible but it never disposes it. Why Hide instead of Close? Why set a local variable to Nothing when it goes out of scope the very next line?
 
Thanks for that sample. I have seen a 'AssemblyLoadEventHandler' delegate in intellisense i've googled for this but there appears to be little info about what it actually does. Is this something to do with splash screens etc.. just wondering.
 
jmcilhinney, was that questions you wanted to know the answer to because you didn't know or was it supposed to be sarcasm of some sort?
 
JohnH said:
jmcilhinney, was that questions you wanted to know the answer to because you didn't know or was it supposed to be sarcasm of some sort?
They were neither really, and no offence was meant. They were just rhetorical questions to make a point, but if there is a legitimate reason for doing either then by all means state it. I could just as easily have made statements instead of using questions: "Call Close instead of Hide. Setting a local variable to Nothing when it goes out of scope immediately afterwards serves no useful purpose". Some people like to set local variables to Nothing "for completeness", although my experience is that they are usually ex-VB6 programmers, where I believe setting a variable to Nothing had the equivalent effect of disposing it. Setting a local variable to Nothing once you've finished with it serves no useful purpose in VB.NET unless that variable doesn't lose scope for some time and you want to release its memory, which may or may not happen any time soon anyway.
 
Ok. Be it the form.close or someobject.close implicitly also calls for disposal is not an excuse for not calling Dispose explicitly when it is available. It happens to be that form.close is a shortcut for hide&dispose, but that is not something to rely on for future programming. Close was a practise earlier, implementation of idisposable and use of dispose is considered best practise nowadays.
So "formprogress = Nothing" should have been "formprogress.Dispose()".
We are not the ones to delve into the inner behaviour of GarbageCollector however it was before, is now, or will be in next generation.
 
JohnH said:
Be it the form.close or someobject.close implicitly also calls for disposal is not an excuse for not calling Dispose explicitly when it is available.
Actually, if you follow Microsoft's guidelines then it is. This is from the help/MSDN topic entitled "Implementing a Dispose Method":
MSDN said:
For types where calling a Close method is more natural than calling a Dispose method, add a public Close method to the base type. The Close method in turn calls the Dispose method without parameters, which performs the proper cleanup operations.
 
Further, note that calling Close on a form does not necessarily Dispose it. The talk in the previous quote of Close being more "natural" is because the effect of calling Close depends on the situation. Calling Close on a form displayed by calling ShowDialog is equivalent to setting the DialogueResult to Cancel, which does require explicit disposal. If you've displayed a form by calling Show, though, calling Close is sufficient and proper to dispose the form.
 
Since there exists some object.close that actually don't call for disposal, this only calls for confusion.
(one is SqlConnection, where close don't call dispose, but dispose calls close ;) )
So how do I feel about the form today? I may perhaps want to close it, but I absolutely want it disposed. Can I be sure if I close it, it will also be disposed? I don't think so, there is nothing intuitive about that, no more than setting the reference holder to 'Nothing' and hoping it will 'go away' soon. So I dispose it (after I have taken it's out of users sight), and know it is disposed asap.
 
Being as it's my first time playing around with 'Sub Main' i take it from a post way at the top of this thread that you can choose what to load next when opening your app i.e dll's etc..
Have either of you got any links or suggestions on the best way to go about this? thanks.
 
I don't think you can control the loading of libraries referenced by the project, but you do have the initialization step when loading components/object created from these references. The actual InitializeComponent sub is a no-touch area, writing into this could cause trouble, and could also very well just be removed by the "Designer".
The sub New call this InitializeComponent and you can tell user whats going on from here on. Here is a modification of the original example (way up there..) where I start telling the user loading process from before the components loads:
VB.NET:
Public Class Form1
  Inherits System.Windows.Forms.Form
 
  'global variable available to both Sub New and Form_load
  'is assigned a new instance object by Sub New
  Dim formprogress As Form2
 
#Region " Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
 
    formprogress = New Form2 'new progress-form object
    formprogress.progressmax = 2
    formprogress.progressvalue = 0
    formprogress.progresstext = "loading (step 1) components"
    formprogress.Show()
    Application.DoEvents()
    Threading.Thread.Sleep(1000) 'just simulating load time..
 
    'This call is required by the Windows Form Designer.
    InitializeComponent()
    'Add any initialization after the InitializeComponent() call
  End Sub
 
  'the rest of the generated code....
 
#End Region
 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
'here I keep on displaying progress messages on the progressform initiated in sub main
    formprogress.progressvalue += 1
    formprogress.progresstext = "loading (step 2) data for instance.."
    Application.DoEvents()
    Threading.Thread.Sleep(1000) 'just simulating load time..
    formprogress.progressvalue += 1
    formprogress.progresstext = "loading (step 3) display setup for instance.."
    Application.DoEvents()
    Threading.Thread.Sleep(1000) 'just simulating load time..
    formprogress.Hide()
    formprogress.Dispose() 
 
  End Sub 'this is where the main form is fully loaded and will display
 
End Class
 
Back
Top