threading with background worker C# win forms

B

bilosta

I have one MainFORM form (it hase dataGridView and button).
one class called DoEverithing (works with data base)
Form PleaseWait (with progress bar)

Here is my problem:
When I click button in Main form i want to populate datagridview with
some data from database.

here is a call:
in MainFORM:
-----------------------------------------------------------------------------------
DoEverithing myDo = new DoEverithing();
private void Button1_Click(object sender, EventArgs e)
{
//call the Please wait form
PleaseWait myPlease = new PleaseWait(this);
myPlease.ShowDialog();
}

public void Populate()
{
dataGridView1.DataSource = myDo.Populate();//it returns dataTable
}
----------------------------------------------------------------------------------

in PleseWait form
----------------------------------------------------------------------------------
public partial class PleaseWait : Form
{
MainFORM myMain;
public MolimSacekajte(MainFORM myMain)
{
this.myMain = myMain;
InitializeComponent();
backgroundWorker1.RunWorkerAsync();

}

private void DOWork(object sender, DoWorkEventArgs e)
{
myMain.Populate();
}

private void zavrsi(object sender, RunWorkerCompletedEventArgs
e)
{
this.Close();
}
----------------------------------------------------------------------------------


I get an error "Cross-thread operation not valid" for dataGridView1
control, how can i repair this. Please help
or tell me another way how to show please wait while dataGridView
populete
Thanx in advance OGA
 
S

Stoitcho Goutsev \(100\)

Hi,

You can prepare the data source in the background thread, but when it comes
to set the new datasource to the datagrid.DataSource property do it in the
main UI thread. You can do that by marshaling the call to the UI thread
using Control.Invoke method.

If you don't know how to use Control.Invoke, please google the news groups
for it. Questions on this methods have been asked so many times that I don't
want to post one more.
 

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