Add control to Panel from another thread, not UI thread

  • Thread starter Alhambra Eidos Kiquenet
  • Start date
A

Alhambra Eidos Kiquenet

Hi misters,

I using BackgroundWorker 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.ComponentModel.DoWorkEventArgs) Handles bgCargaFichero.DoWork

Try

'SplitContainer1.Panel1.SuspendLayout()

'SplitContainer1.Refresh()



' Procesar fichero

Me.ProcesarFicheroCargadoWork() ' IN THIS METHOD I CREATE CONTROLS Type =
ContenedorVisorBase



'SplitContainer1.Panel1.ResumeLayout(False)



If bgCargaFichero.CancellationPending = 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.ComponentModel.ProgressChangedEventArgs) Handles
bgCargaFichero.ProgressChanged



Dim contenedorPagina As
GRUPOBACKUP.Administrador.Util.Cliente.ControlesWindows.ContenedorVisorBase =
Nothing

contenedorPagina = CType(e.UserState,
GRUPOBACKUP.Administrador.Util.Cliente.ControlesWindows.ContenedorVisorBase)

If contenedorPagina IsNot Nothing Then

AddMiniaturaToPanel(contenedorPagina)

End If



=====



Delegate Sub AddMiniaturaToPanelDelegate2(ByVal cCTL As Control)



Private Sub AddMiniaturaToPanel(ByVal cCTL As Control)

If cCTL.InvokeRequired Then

Dim d As New AddMiniaturaToPanelDelegate2(AddressOf AddMiniaturaToPanel)

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

Else

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

End If

End Sub
 
M

Marc Gravell

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top