Adding ActiveX controls at runtime and threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Toe Dipper

    #1

    Adding ActiveX controls at runtime and threading

    In short we have a lengthy process when a form is loaded that adds
    activex controls to our windows form. This process in itself works
    fine however we would like to push this processing to a thread but are
    stumped so far.

    Our goal is simply to allow the form to fully display while the
    controls are being added. Preferrably with a progressbar to let them
    know that the form is still being built.

    Here are some snippets.

    Private Sub Form_Shown(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles Me.Shown
    Dim myThread As Threading.Threa d
    myThread = New Threading.Threa d(AddressOf load_udfSection s)
    myThread.SetApa rtmentState(Thr eading.Apartmen tState.STA)
    myThread.Start( )
    End Sub

    Private Sub load_udfSection s()
    ...
    udfSection = New AxTdF_DynamicUD F.AxDynUDF

    udfSection.Begi nInit()
    udfSection.Anch or =
    CType(((System. Windows.Forms.A nchorStyles.Top Or
    System.Windows. Forms.AnchorSty les.Left) _
    Or System.Windows. Forms.AnchorSty les.Right),
    System.Windows. Forms.AnchorSty les)
    udfSection.Enab led = True
    udfSection.Visi ble = False
    udfSection.Loca tion = New
    System.Drawing. Point(ToolStrip Container1.Cont entPanel.Margin .Left,
    currentTop)
    udfSection.Name = "AxDynUDF" & CStr(controlInd ex + 1)

    With ToolStripContai ner1.ContentPan el
    axWidth = .Width - (.Margin.Left + .Margin.Right)
    End With

    udfSection.Size = New System.Drawing. Size(axWidth, 1)

    ToolStripContai ner1.ContentPan el.Invoke(New
    addControl(Addr essOf addControlToToo lstrip), udfSection)
    udfSection.EndI nit()

    udfSection.load _Controls(CShor t(reader("Contr olGroupID")), True)

    .....
    End Sub


    There is a delegate called addcontroltoToo lstrip for the following
    function.

    Private Sub addControlToToo lstrip(ByVal activexControl As
    AxTdF_DynamicUD F.AxDynUDF)
    ToolStripContai ner1.ContentPan el.Controls.Add (activexControl )
    End Function

    The invoke on the toolstrip seems to work fine but then when we try to
    call endinit() it errors telling us that the control has been modified
    in a different thread.

    Any ideas? Only been using .NET for a week so there is no chance of
    offending me. In VB6 we would have just started a timer in the show
    event of the form which would let the event exit and then the timer
    would process our lengthy code. Ugly but it at least allowed the form
    to fully display before adding the remaining controls. Looking for
    something a little bit more interactive.

    Glenn Welker
  • gene kelley

    #2
    Re: Adding ActiveX controls at runtime and threading

    On Mon, 26 Jun 2006 08:47:53 -0400, Toe Dipper
    <toedipper@disc ussions.microso ft.com> wrote:
    [color=blue]
    >In short we have a lengthy process when a form is loaded that adds
    >activex controls to our windows form. This process in itself works
    >fine however we would like to push this processing to a thread but are
    >stumped so far.
    >
    >Our goal is simply to allow the form to fully display while the
    >controls are being added. Preferrably with a progressbar to let them
    >know that the form is still being built.
    >
    >Here are some snippets.
    >
    > Private Sub Form_Shown(ByVa l sender As Object, ByVal e As
    >System.EventAr gs) Handles Me.Shown
    > Dim myThread As Threading.Threa d
    > myThread = New Threading.Threa d(AddressOf load_udfSection s)
    > myThread.SetApa rtmentState(Thr eading.Apartmen tState.STA)
    > myThread.Start( )
    > End Sub
    >
    >Private Sub load_udfSection s()
    > ...
    > udfSection = New AxTdF_DynamicUD F.AxDynUDF
    >
    > udfSection.Begi nInit()
    > udfSection.Anch or =
    >CType(((System .Windows.Forms. AnchorStyles.To p Or
    >System.Windows .Forms.AnchorSt yles.Left) _
    > Or System.Windows. Forms.AnchorSty les.Right),
    >System.Windows .Forms.AnchorSt yles)
    > udfSection.Enab led = True
    > udfSection.Visi ble = False
    > udfSection.Loca tion = New
    >System.Drawing .Point(ToolStri pContainer1.Con tentPanel.Margi n.Left,
    >currentTop)
    > udfSection.Name = "AxDynUDF" & CStr(controlInd ex + 1)
    >
    > With ToolStripContai ner1.ContentPan el
    > axWidth = .Width - (.Margin.Left + .Margin.Right)
    > End With
    >
    > udfSection.Size = New System.Drawing. Size(axWidth, 1)
    >
    > ToolStripContai ner1.ContentPan el.Invoke(New
    >addControl(Add ressOf addControlToToo lstrip), udfSection)
    > udfSection.EndI nit()
    >
    > udfSection.load _Controls(CShor t(reader("Contr olGroupID")), True)
    >
    > .....
    > End Sub
    >
    >
    >There is a delegate called addcontroltoToo lstrip for the following
    >function.
    >
    >Private Sub addControlToToo lstrip(ByVal activexControl As
    >AxTdF_DynamicU DF.AxDynUDF)
    > ToolStripContai ner1.ContentPan el.Controls.Add (activexControl )
    > End Function
    >
    >The invoke on the toolstrip seems to work fine but then when we try to
    >call endinit() it errors telling us that the control has been modified
    >in a different thread.
    >
    >Any ideas? Only been using .NET for a week so there is no chance of
    >offending me. In VB6 we would have just started a timer in the show
    >event of the form which would let the event exit and then the timer
    >would process our lengthy code. Ugly but it at least allowed the form
    >to fully display before adding the remaining controls. Looking for
    >something a little bit more interactive.
    >
    >Glenn Welker[/color]

    Based on what you posted, you are essentially continuing what would
    normally happen in the Load Event, so I'm not sure why you want to use
    a separate thread to continue loading controls. Is there some other
    background process going on that is not shown in your code?

    In VB6, I used a similar concept which worked fine - show the main
    form ASAP, then continue with the load event. In VB2005, my
    experience, so far, hs been that this is a bit more difficult to
    implement from app to app as, in some cases, the main form has a
    tendency to do an undesirable repaint when adding and positioning
    controls after the main form is visible particularly when I have code
    in the Layout Event.

    Gene

    Comment

    • Toe Dipper

      #3
      Re: Adding ActiveX controls at runtime and threading

      [color=blue]
      >
      >Based on what you posted, you are essentially continuing what would
      >normally happen in the Load Event, so I'm not sure why you want to use
      >a separate thread to continue loading controls. Is there some other
      >background process going on that is not shown in your code?
      >
      >In VB6, I used a similar concept which worked fine - show the main
      >form ASAP, then continue with the load event. In VB2005, my
      >experience, so far, hs been that this is a bit more difficult to
      >implement from app to app as, in some cases, the main form has a
      >tendency to do an undesirable repaint when adding and positioning
      >controls after the main form is visible particularly when I have code
      >in the Layout Event.
      >
      >Gene[/color]

      We were really just trying to clean up a hack that has been in our
      code too long.

      You are right we are trying to continue the load event. The code I
      have referenced takes up to 10 seconds. Without any progress
      indication to the user, it appears that the form has locked up. It
      seemed like I could load the controls in a background process while
      updating a progress bar in the main thread. This would have enabled
      the user to move the gui and get feedback on the progress while the
      background process finished.

      Our solution for VB6 seems to be less robust under vb 2005. Since the
      documentation for doevents points to multithreading it seems possible,
      although quite complicated.

      I appreciate your feedback.

      Comment

      • gene kelley

        #4
        Re: Adding ActiveX controls at runtime and threading

        On Tue, 27 Jun 2006 09:13:18 -0400, Toe Dipper
        <toedipper@disc ussions.microso ft.com> wrote:
        [color=blue]
        >[color=green]
        >>
        >>Based on what you posted, you are essentially continuing what would
        >>normally happen in the Load Event, so I'm not sure why you want to use
        >>a separate thread to continue loading controls. Is there some other
        >>background process going on that is not shown in your code?
        >>
        >>In VB6, I used a similar concept which worked fine - show the main
        >>form ASAP, then continue with the load event. In VB2005, my
        >>experience, so far, hs been that this is a bit more difficult to
        >>implement from app to app as, in some cases, the main form has a
        >>tendency to do an undesirable repaint when adding and positioning
        >>controls after the main form is visible particularly when I have code
        >>in the Layout Event.
        >>
        >>Gene[/color]
        >
        >We were really just trying to clean up a hack that has been in our
        >code too long.
        >
        >You are right we are trying to continue the load event. The code I
        >have referenced takes up to 10 seconds. Without any progress
        >indication to the user, it appears that the form has locked up. It
        >seemed like I could load the controls in a background process while
        >updating a progress bar in the main thread. This would have enabled
        >the user to move the gui and get feedback on the progress while the
        >background process finished.
        >
        >Our solution for VB6 seems to be less robust under vb 2005. Since the
        >documentatio n for doevents points to multithreading it seems possible,
        >although quite complicated.
        >
        >I appreciate your feedback.[/color]

        A common technique for a long load time (10 seconds is long) is to
        display a sort of spash screen (not the VB2005 splash screen) as soon
        as the main form is displayed where the splash form contains a label
        that is updating iindicating to the user that there is activity.
        PhotoShop is a good example if you are familiar with that program.

        Gene

        Comment

        Working...