Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB/C#.net Dynamically Add Control Items with Background Worker

I'm creating a Windows Form application which Dynamically creates controls based on data pulled from a Database.

I have the code working great in the background which loads the data from the database and applies it to variables, the problem I am having is when trying to create the controls using this data, I get a multi-threading error ( Additional information: Cross-thread operation not valid: Control 'flowpanelMenuRules' accessed from a thread other than the thread it was created on.)

I'm using the BackgroundWorker_DoWork event and the code that fails is the following:

Me.flowpanelMenuRules.Controls.Add(PanelRule(i))

The code before is a simple loop going through the variable (which is pulled from the database) and gathering the information.

Has anybody had any experience in safely invoking the above line? I just can't seem to get it to work at all :(

Thanks for the help, I can post more code if needed.

like image 667
Richard C Avatar asked Mar 27 '16 18:03

Richard C


People also ask

What is VB in C?

Visual Basic (also known as VB) is an event driven programming language. This is the third generation of such language and is also an integrated development environment (or IDE). It comes from Microsoft and is used specifically for its programming model –COM.

Is VB and C# the same?

Though C# and VB.NET are syntactically very different, that is where the differences mostly end. Microsoft developed both of these languages to be part of the same . NET Framework development platform. They are both developed, managed, and supported by the same language development team at Microsoft.

Is Visual Basic based on C?

Visual Basic can also be used within other Microsoft software to program small routines. Visual Basic was succeeded in 2002 by Visual Basic . NET, a vastly different language based on C#, a language with similarities to C++.

Is .NET C# or VB?

C# is commonly pronounced as C-sharp. It is the object-oriented programming language that is run on the . NET framework. This language is developed by Microsoft.


2 Answers

My recommendation is to have your BackgroundWorker simply create the controls, but not add them to the form. Instead, return the prepared controls to the calling/UI thread via the RunWorkerCompleted event. Then add the controls to your form at this point, possibly in conjunction with the SuspendLayout()/ResumeLayout() methods.

like image 51
Joel Coehoorn Avatar answered Nov 14 '22 21:11

Joel Coehoorn


The native way for WinForms application is to use System.Winfows.Forms.Control methods like Invoke() and InvokeRequired property to accessing UI thread:

if(this.flowpanelMenuRules.InvokeRequired)
{
    this.flowpanelMenuRules.Invoke(() => AddPanelRule());
}
else
{
    AddPanelRule();
}

You can also use Dispatcher class.

If you are sure to be on a UI thread (e.g. in an button.Click handler), Dispatcher.CurrentDispatcher gives you the UI thread dispatcher that you can later use to dispatch from background threads to the UI thread as usual.

Dispatcher.CurrentDispatcher.Invoke(() => this.flowpanelMenuRules.Controls.Add(PanelRule(i)));
like image 40
Vadim Martynov Avatar answered Nov 14 '22 23:11

Vadim Martynov