Can't we simultaneously instantiate multiple forms?
I used different backgroundworkers to instantiate some 'heavy' forms thinking that it will reduce the time to instantiate them if I did them at once but it looks like I am wrong since it is still taking about 43 seconds to load all of them. Am I missing something or do you have a tip on how I can speed up their instantiation?
TIA
Re: Can't we simultaneously instantiate multiple forms?
You can't really create forms using a BackgroundWorker anyway because then each form is owned by a different thread. Any communication between them would require invocation of a delegate.
As for the speed, it might depend on how many tasks are actually being performed concurrently and how many are interleaved. Sometimes multi-threading can actually make things take longer because of the overhead of context switching.
Re: Can't we simultaneously instantiate multiple forms?
The forms are MDIChilds and yes I am using delegates to set the MDIParent. I am doing the instantiation upong logging-in and while the Splash form is shown. The code is just as simple as
Code:
Form1 frm = new Form1()
frm.MDIParent = this
I thought there should be a way to intantiate them all at once, is that not possible?
Re: Can't we simultaneously instantiate multiple forms?
No it's no possible. Think about it. Obviously an MDI parent form would have to be able to access its MDI children. Accessing a control from any thread other than the one that created it is illegal though, so how could the parent possibly access the children if they were all created on different threads?
Re: Can't we simultaneously instantiate multiple forms?
Actually it is working, I am getting the forms by this.MdiChildren and it is listed there. Using a delegate for the "MDIParent = this" works, I am wondering why you are thinking that it is not possible? Am I missing something?
My only concern is to speed things up.
Re: Can't we simultaneously instantiate multiple forms?
Can you show the code you're using? I think you'll find that you are not actually doing what you think you're doing. That's possibly why you didn't see any time difference.
Re: Can't we simultaneously instantiate multiple forms?
It looks like this. Is this what you are looking for or the code for the backgroundworker? I am just calling this procedure from the DoWork procedure of the backgroundworker.
Code:
Private Sub InstantiateForm()
If Me.InvokeRequired Then
Dim d As New InstantiateFormCallback(InstantiateForm)
Me.Invoke(d, Nothing)
Else
Dim frm As Form1 = New Form()
frm.MdiParent = Me
End If
End Sub
Re: Can't we simultaneously instantiate multiple forms?
When you call Me.Invoke follow it through and look at the callstack, you should see that the thread you are on is actually the main thread, it will just block your current thread until the main thread is avail, then use the main thread, which in turn blocks all the other threads and hands back the calling thread when it is finished.
Re: Can't we simultaneously instantiate multiple forms?
To add, you can suspend the layout of forms during heavy load to stop screen-redraws/resizing. Lookup Suspendlayout/resumelayout. There is also an API to lock a window which I have had to use in the past to remove drawing while resizing mdi forms in a complex tab interface.
Re: Can't we simultaneously instantiate multiple forms?
So you mean to say what I want is not possible?
Re: Can't we simultaneously instantiate multiple forms?
Errrr...correct. The only way I can think of is to use multiple processes and then use api's to drag windows across, but it would be really really really bad :). I would work on speeding up your loading times.
Re: Can't we simultaneously instantiate multiple forms?
Quote:
Originally Posted by
Grimfort
To add, you can suspend the layout of forms during heavy load to stop screen-redraws/resizing. Lookup Suspendlayout/resumelayout. There is also an API to lock a window which I have had to use in the past to remove drawing while resizing mdi forms in a complex tab interface.
Will try Suspendlayout/Resumelayout if it will provide some improvement.
Re: Can't we simultaneously instantiate multiple forms?
After profiling the controls I observe that this one 3rd party control (numericbox) tends to slow the loading of the forms and the usercontrols. I replaced them with plain textboxes and the speed improved significantly. From more than 45+ seconds it is now playing between 13-18 seconds to instantiate those forms. It is much faster now and I can alreeady lazy load the usercontrols which is not immediately needed.
I will keep this thread open since my original question was not answered satisfactorily or that it is ill advised to do so. If anyone have some recommendations for speeding things up then they are still very much welcome to share.
Re: Can't we simultaneously instantiate multiple forms?
Do you need to load all the forms on startup? Can't you load as and when required, or at least load them up without data?
Re: Can't we simultaneously instantiate multiple forms?
They are only instantiated and hidden. The Load event is not fired hence they don't look loaded to me. =)
Re: Can't we simultaneously instantiate multiple forms?
I'll rephrase, do you need to create instances of all these forms? Or are you just trying to save the user waiting while moving around your app?