Add control to Panel from another thread, not UI thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=

    Add control to Panel from another thread, not UI thread

    Hi misters,

    I using BackgroundWorke r control in my Windows Forms. In my form, I have a
    SplitterPanel.

    In DoWork event , I create controls child, and in ProgressChanged event I
    try add controls to SplitterPanel, but the application not responds. I use
    InvokeRequired for controls child.

    Any suggestions, please ?

    Thanks in advance.



    Private Sub bgCargaFichero_ DoWork(ByVal sender As System.Object, ByVal e As
    System.Componen tModel.DoWorkEv entArgs) Handles bgCargaFichero. DoWork

    Try

    'SplitContainer 1.Panel1.Suspen dLayout()

    'SplitContainer 1.Refresh()



    ' Procesar fichero

    Me.ProcesarFich eroCargadoWork( ) ' IN THIS METHOD I CREATE CONTROLS Type =
    ContenedorVisor Base



    'SplitContainer 1.Panel1.Resume Layout(False)



    If bgCargaFichero. CancellationPen ding = True Then

    e.Cancel = True

    Else

    e.Result = True

    End If

    End Sub





    Private Sub bgCargaFichero_ ProgressChanged (ByVal sender As System.Object,
    ByVal e As System.Componen tModel.Progress ChangedEventArg s) Handles
    bgCargaFichero. ProgressChanged



    Dim contenedorPagin a As
    GRUPOBACKUP.Adm inistrador.Util .Cliente.Contro lesWindows.Cont enedorVisorBase =
    Nothing

    contenedorPagin a = CType(e.UserSta te,
    GRUPOBACKUP.Adm inistrador.Util .Cliente.Contro lesWindows.Cont enedorVisorBase )

    If contenedorPagin a IsNot Nothing Then

    AddMiniaturaToP anel(contenedor Pagina)

    End If



    =====



    Delegate Sub AddMiniaturaToP anelDelegate2(B yVal cCTL As Control)



    Private Sub AddMiniaturaToP anel(ByVal cCTL As Control)

    If cCTL.InvokeRequ ired Then

    Dim d As New AddMiniaturaToP anelDelegate2(A ddressOf AddMiniaturaToP anel)

    Me.Invoke(d, New Object() {cCTL})

    Else

    SplitContainer1 .Panel1.Control s.Add(cCTL) ' Here, the application not
    responds !!!!

    End If

    End Sub






    --





  • Marc Gravell

    #2
    Re: Add control to Panel from another thread, not UI thread

    This came up yesterday (I'll look for the post); "no" is the short
    answer. Only the UI thread can talk to the UI; and only the UI thread
    should be creating controls.

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Add control to Panel from another thread, not UI thread

      I'll look for the post

      It was, in fact, you. Believe it or not, nothing has changed overnight
      - the same answers still apply...



      Marc

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Add control to Panel from another thread, not UI thread


        Hi,

        First of all this is a C# NG, not VB.NET

        and the answer is no, you cannot do it, you have to send the event to
        the UI thread using Control.Invoke

        Comment

        Working...