Results 1 to 8 of 8

Thread: Design Time or Run Time?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Design Time or Run Time?

    I have had this question always in my mind. Imagine I have at least 5 dataadapters to be added to my form and in the life cycle of the form I need to refere to them from time to time. So i have three choices:
    1-Add them in desgin time then when the form is intialized the connection, dataadapters and dataset are intialized and kept active until I close the form and all disposed.
    2-Add them at run time and do not dispose them then it has the same impact as the previous one.
    3-Add them at run time when needed and then dispose them when finished with them, and repeat this each time needed.

    Which one of the above solutions you suggest? and why? I just bring dataadapters as example, it can be any object.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I personally like to keep a tight reign on my objects, and only instantiate them when I need to use them, and dispose of them immediately when I am done with them.

    However, sometimes this means using late binding vs. early binding - if using polymorphism such as:
    VB Code:
    1. Dim myObj As Object
    Above I decare myObj to be of type object, then at run time I instantiate the object (New) to the type of Object that I wanted (in my case a particular form). This saves me from declaring each form at design time, and then not even using most of them - or at the very least it saves me from typing the declaration of:
    VB Code:
    1. Dim myobj1 as Form1


    Late binding incurrs a performance hit because the compiler can't perform it's optimizations, type checking, and member lookup. So more work has to be done at runtime. However, I don't think the performance hit is significant (although I have not actually tested it, but doesn't seem to slow it down).

  3. #3
    Junior Member
    Join Date
    Apr 2003
    Posts
    18
    In most cases with VB.NET there will be little to no difference thanks to managed code and garbage collection. This of course is dependant on the object and its dependencies. Application or Form load time may need to be considered. Should you decided to create and destroy your objects at run time. Remember that .NET uses garbage collection and may not truly destroy the object. You can use "gc.Collect" to force garbage collection though is not recommended unless you have a long lived application that goes idle for sometime.
    www.WinMgmt.com
    [email protected]

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I would say late binding should be avoided at most costs. Just running a simple test:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim tStart As DateTime = DateTime.Now
    3.         Dim obj As Object
    4.         obj = New Form4()
    5.         obj.Show()
    6.         Dim elapsed1 As Single = Now.Subtract(tStart).TotalMilliseconds
    7.         tStart = DateTime.Now
    8.         Dim frm As Form4
    9.         frm = New Form4()
    10.         frm.Show()
    11.         Dim elapsed2 As Single = Now.Subtract(tStart).TotalMilliseconds
    12.  
    13.         MsgBox(String.Format("Object: {0}{1}Form4: {2}", elapsed1, ControlChars.NewLine, elapsed2))
    14.  
    15.     End Sub

    Shows that late binding is upto 6 times slower and this is just for simple objects, not to mention the fact that this takes away some of the error checking done during compiling. If its relevant to the project I say go for it, but if you know the object type ahead of time then don't. You could say that today's machines are fast enough and even in this test the difference is in milliseconds, but why slow things done without need.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually my little test revealed more than I thought. Try changing it to this:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim tStart As DateTime = DateTime.Now
    3.         Dim obj As Object
    4.         obj = New Form4()
    5.         obj.Show()
    6.         obj.close()
    7.         Dim elapsed1 As Single = Now.Subtract(tStart).TotalMilliseconds
    8.         tStart = DateTime.Now
    9.         Dim frm As Form4
    10.         frm = New Form4()
    11.         frm.Show()
    12.         frm.Close()
    13.         Dim elapsed2 As Single = Now.Subtract(tStart).TotalMilliseconds
    14.  
    15.         MsgBox(String.Format("Object: {0}{1}Form4: {2}", elapsed1, ControlChars.NewLine, elapsed2))
    16.     End Sub

    The first time the code runs there is a large difference but if you continuely run it then there is no difference after that, unless you restart the app. Which although it counters some of what I said, it also shows how cool the MSIL compiler bit is, and the effects of the garbage collector.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thanks for your input. So you mean that If i know the object type I try to add them in design time?
    You can use "gc.Collect" to force garbage collection though is not recommended unless you have a long lived application that goes idle for sometime.
    In fact this is what i am facing. In one of my projects I have a Crystal Report page that the user may just be using that form for more than 1 hour or so trying to print out the pages that is sometimes more than 1500 pages(The temp file of this report may be as large as 700 mb). So when this form is going to close and return to the main page it takes a little more time than if you quickly close it and not let it idle for long time. So do you recommend that I try to invoke GC on another thread while the user is busy with printing? and if yes, then at what interals? how can I guess when is the right time?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i dont know if i am 100% right but using GC.Collect wont clean up any resources..the only thing it will do is set the objects as ready for finalization so next time the garbage collector executes it will delete the object

    http://www.gotdotnet.com/team/librar...#_Toc522530574
    \m/\m/

  8. #8
    Junior Member
    Join Date
    Apr 2003
    Posts
    18
    Lunatic3,

    based on everything I have seen gc.colllect tells the GC that now is the best time to clean and reallocate memeory. Which is why I only reccommed this procedure on apps that are idle for some time.

    The garbage collector's optimizing engine determines the best time to perform a collection
    From my experience "gc.collect" forces this to happen.
    www.WinMgmt.com
    [email protected]

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