I have winForm that needs update Panel (add controls to Panel of
SplitContainer dynamically).
I have a Backgroundworker, and DoWork event I want to add controls but I get
this error in Spanish (I don't get translation: Los controles creados en un
subproceso no pueden tener controles primarios en un control en un subproceso
diferente.)
CheckForIllegalCrossThreadCalls = False; /* !!!! */
Me.bgCargaFichero.WorkerReportsProgress = True;
Me.bgCargaFichero.WorkerSupportsCancellation = True;
Me.bgCargaFichero.RunWorkerAsync();
Private bgCargaFichero_DoWork(...) //Handles bgCargaFichero.DoWork
{
SplitContainer1.Panel1.SuspendLayout();
for ...
{
SplitContainer1.Panel1.Controls.Add(contenedorPagina);
}
SplitContainer1.Panel1.ResumeLayout(False);
If bgCargaFichero.CancellationPending = True
e.Cancel = True;
else
e.Result = True;
}
Can I add controls to a Panel from another process ??
Thanks in advance. Greetings
--
http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.net
http://www.setbb.com/putainformatica/viewtopic.php?p=843
www.trabajobasura.com/solusoft
And the answer is no. UI elements like controls have "thread affinity";
only the UI thread sould create them, update them, or even read from
them. The only legitimate cross-threaded operations are things like
InvokeReqired, Invoke and BeginInvoke (to switch to the UI thread).
Marc
From what I can understand from the error message, you create the control in
your backgroundworker thread. Controls need to belong to the main GUI
thread, but there is nothing stopping you from triggering such creation from
another thread, but before you create and add the control you must have
passed through an Invoke
Typicall you would do something like this
public void CreateControlInAsynkThread()
{
CreateControlInGuiThread();
}
// This method should exist in the form or usercontrol file
public void CreateControlInGuiThread()
{
if(InvokeRequired)
{
Invoke(new MethodInvoker(CreateControlInGuiThread));
return; // Important to return as this code runs asyncronously
}
// If we reach this code we are in the gui thread and can manipulate the
gui
Button b = new Button();
panel1.Controls.Add(b);
}
Replace MethodInvoker with the proper delegate if needed.
--
Happy Coding!
Morten Wennevik [C# MVP]
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 ?
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
> 'SplitContainer1.Refresh()
internal void ThreadedRefresh()
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.InvokeRequired)
{
SetRefreshCallback d = new SetRefreshCallback
(ThreadedRefresh);
Invoke(d, new object[] {});
}
else
{
MyControl.Visible = true;
Refresh(); // refresh whole screen! use a container?
}
}