473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

add controls from another process using backgroundworker

Hi misters,

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(contenedorPagi na);
}
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...opic.php?p=843
www.trabajobasura.com/solusoft

Aug 7 '08 #1
4 4780
Firstly, I doubt that this is cross-process; it is just a different thread.

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
Aug 7 '08 #2
Hi Alhambra,

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]
"Alhambra Eidos Kiquenet" wrote:
Hi misters,

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(contenedorPagi na);
}
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...opic.php?p=843
www.trabajobasura.com/solusoft
Aug 7 '08 #3
Mister, my code is similar like this:
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.ControlesWi ndows.ContenedorVisorBase =
Nothing

contenedorPagina = CType(e.UserState,
GRUPOBACKUP.Administrador.Util.Cliente.ControlesWi ndows.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

Aug 8 '08 #4
On Thu, 07 Aug 2008 23:56:01 -0700, Alhambra Eidos Kiquenet wrote:
'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?
}
}

Aug 8 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Daniel Walzenbach | last post by:
Hi, I want to display a Label in a DataGrid according to some condition. I therefore check whether the condition is true in the ItemDateBound EventHandler of the DataGrid. Unfortunately the...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
6
by: Kyote | last post by:
I'm trying to make, what I thought, would be a simple application. This application will help me organize files I have a lot of, on my computer(ebooks, pictures, etc..). I'm currently dealing with...
4
by: Diego F. | last post by:
Hello. I have a process that can take a long time and I'm trying to put an animated gif while the user is waiting. I added a panel with a picturebox with the image inside. The panel is not...
5
by: Manikandan | last post by:
Hi, My application is processing a database(selecting data using execute reader) It takes much time. I want to show user the application is running, showing some information in the form I tried...
3
by: rob | last post by:
I've got a couple stretches of code where I use a lot of CPU on various operations. Since this is happening in a loop at unrestriicted speed, some of the form controls do not update. (Actually...
13
by: Johnny Jörgensen | last post by:
Hi I have a procudure that takes some time and thus slows down the main system. I want to put it in a backgroundworker component to run it asynchroneously. But in the procedure, I want to update a...
32
by: John Wright | last post by:
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. ...
4
by: =?Utf-8?B?U3ZlbiBXLg==?= | last post by:
Hi Newsgroup, I am developing a C# Windows Forms application that launches processes within a background worker.. The processes seem to have a memory limit of about 278mb. Some proccesses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.